C# Problem with Oauth
I am trying to create oauth signature. But I dont know what I am doing wrong because the site giving unauthorize error. I am using oauth version 1.0. Method is HMAC-SHA1 and it is google based oauth. My base string is correct because it checked it with sample output. My code :
string oauthSig = "";
string baseString = HttpUtility.UrlEncode(httpMethod.ToUpper()) + "&" +开发者_如何学C
HttpUtility.UrlEncode(url) + "&" +
HttpUtility.UrlEncode("oauth_callback="+callback+"&"+
"oauth_consumer_key="+consumerKey+"&"+
"oauth_nonce="+nounce+"&"+
"oauth_signature_method="+sigMethod+"&"+
"oauth_timestamp=" + timestamp + "&" +
"oauth_version=" + version
);
HMACSHA1 myhmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(consumeSecret)),true);
byte[] hashValue = myhmacsha1.ComputeHash(Encoding.UTF8.GetBytes(baseString));
oauthSig = Convert.ToBase64String(hashValue);
Please tell me if I am doing anything wrong.
Thanks
The key to the signature should be:
CONSUMER_SECRET + '&' + TOKEN_SECRET
And since you do not have a token secret yet, you should use CONSUMER_SECRET and an ampersand (&) as the key to the signature.
Edit, further clarification:
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));
byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(data);
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
return Convert.ToBase64String(hashBytes);
I've not tested the code but i've taken it from from oauth.googlecode.com - OAuthBase.cs. I highly recommend checking it out, it should do everything you want.
精彩评论