C # Problem with Oauth

I am trying to create an oauth signature. But I do not know what I am doing wrong, because the site gives an unauthorized error. I am using oauth version 1.0. The method is HMAC-SHA1, and it is based on google. My base line is correct because she checked it with sample output. My code is:

string oauthSig = "";
        string baseString = HttpUtility.UrlEncode(httpMethod.ToUpper()) + "&" +
                            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 do something wrong.

thank

+3
source share
1 answer

The key to the signature should be:

CONSUMER_SECRET + '&' + TOKEN_SECRET

And since you don't have a secret key yet, you should use CONSUMER_SECRET and ampersand (&) as the key to the signature.

Modify, 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);

, oauth.googlecode.com - OAuthBase.cs. , , .

+4

Source: https://habr.com/ru/post/1782748/


All Articles