Note that there are some explanatory texts on larger screens.

plurals
  1. POTwitter request token returns 401 unauthorized. I cant find what I did wrong
    text
    copied!<p>I need to do twitter sign in for mobile apps. Thus far I have tried a lot of libraries including tweetsharp and linq2twitter. But I am getting nowhere. With all this trouble, I resorted to webclient calls, but I am once again stuck. When I make the call, I get the 401 Unauthorized error. I include my code here. Can you guys see anything wrong at all ? I am really stuck with this.</p> <pre><code> string oauth_signature_method = "HMAC-SHA1"; TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString(); string oauth_version = "1.0"; string oauth_consumer_key = "iD1232134AQ2Pb6Q"; string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())); SortedDictionary&lt;string, string&gt; sd = new SortedDictionary&lt;string, string&gt;(); sd.Add("oauth_version", oauth_version); sd.Add("oauth_consumer_key", oauth_consumer_key); sd.Add("oauth_nonce", oauth_nonce); sd.Add("oauth_signature_method", oauth_signature_method); sd.Add("oauth_timestamp", oauth_timestamp); UrlEntity callback = new UrlEntity(); callback.Url = @"//tweeter"; string encodedCallbackUrl = HttpUtility.UrlEncode(callback.Url); sd.Add("oauth_callback",encodedCallbackUrl); WebClient wc = new WebClient(); wc.Headers.Add("User-Agent: randomAgent HTTP Client"); wc.Headers.Add("Host: api.twitter.com"); wc.Headers.Add(@"Accept: */*"); UrlEntity url = new UrlEntity(); url.Url = @"https://api.twitter.com/oauth/request_token"; string signature = CreateSignature(url, sd); sd.Add("oauth_signature",signature); string dataValues = ""; foreach (KeyValuePair&lt;string, string&gt; pair in sd) { dataValues += pair.Key + "=''" + pair.Value + "'',"; } dataValues = dataValues.Substring(0, dataValues.Length - 1); // cuts off the last, string headerVal = " Oauth " + dataValues; wc.Headers.Add("Authorization",headerVal); wc.UploadString(@"https://api.twitter.com/oauth/request_token", ""); wc.DownloadStringCompleted += WcOnDownloadStringCompleted; </code></pre> <p>Below is the code to make the signature.</p> <pre><code>public static string CreateSignature(UrlEntity url, SortedDictionary&lt;string, string&gt; sd) { string parameterString = ""; SortedDictionary&lt;string, string&gt; sd2 = new SortedDictionary&lt;string, string&gt;(); foreach (KeyValuePair&lt;string, string&gt; parameter in sd) { string encodedKey = HttpUtility.UrlEncode(parameter.Key); string encodedvalue = HttpUtility.UrlEncode(parameter.Value); sd2.Add(encodedKey,encodedvalue); } foreach (KeyValuePair&lt;string, string&gt; pair in sd2) { parameterString += pair.Key + "=" + pair.Value + "&amp;"; } parameterString = parameterString.Substring(0, parameterString.Length - 1); // cuts off the last &amp; string signatureString = "POST&amp;"; var encodedUrl = HttpUtility.UrlEncode(url.Url); // percent encode URL signatureString += encodedUrl + "&amp;"; signatureString += HttpUtility.UrlEncode(parameterString); // percent encode entire parameter string string consumerSecret = "supercorectConsumersecretfromtwitterwebsite"; string signingKey = HttpUtility.UrlEncode(consumerSecret) + "&amp;"; HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey)); string signatureStringFinal = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(signatureString))); return signatureStringFinal; } </code></pre> <p>It is a bit messy, but I am stuck. The following links might help. <a href="https://dev.twitter.com/docs/auth/implementing-sign-twitter" rel="nofollow">https://dev.twitter.com/docs/auth/implementing-sign-twitter</a> <a href="https://dev.twitter.com/docs/api/1/post/oauth/request_token" rel="nofollow">https://dev.twitter.com/docs/api/1/post/oauth/request_token</a> <a href="https://dev.twitter.com/docs/auth/creating-signature" rel="nofollow">https://dev.twitter.com/docs/auth/creating-signature</a></p> <p>EDIT:</p> <p>I have taken a look at the request using fiddler. The response message has this in it...</p> <p>"Failed to validate oauth signature and token"</p> <p>Which likely means the oauth signature is wrong. But I have looked and I have not seen anything.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload