C# Oauth 问题

发布于 2024-10-09 09:45:58 字数 1201 浏览 0 评论 0原文

我正在尝试创建 oauth 签名。但我不知道我做错了什么,因为该网站给出了未经授权的错误。我使用的是oauth 1.0版本。方法是 HMAC-SHA1,它是基于 google 的 oauth。我的基本字符串是正确的,因为它使用示例输出对其进行了检查。 我的代码:

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

如果我做错了什么,请告诉我。

谢谢

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()) + "&" +
                            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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2024-10-16 09:45:58

签名的关键应该是:

CONSUMER_SECRET + '&' + TOKEN_SECRET

由于您还没有令牌机密,因此您应该使用 CONSUMER_SECRET 和与号 (&) 作为签名的密钥。

编辑,进一步说明

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。我强烈建议您检查一下,它应该可以满足您想要的一切。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文