gpt4 book ai didi

c# - 使用 HMAC-SHA1 加密创建 OAuth 签名返回 HTTP 401

转载 作者:可可西里 更新时间:2023-11-01 08:47:14 27 4
gpt4 key购买 nike

问题
我需要对需要 OAuth 加密的 API 进行身份验证。

我的方向是正确的,但我确信我的签名基本字符串有问题。由于 HMACSHA1 哈希基于 key 和 BaseString,因此我得到了错误的 oauth_signature。

OAuth Signing Process

到目前为止
我已经能够收集到所有必需的数据,其中包括:

  • Consumer Key
  • Consumer Secret
  • Acces Token
  • Acces Secret
  • Sha1Hased Value (Based on Key and Message, where Message is the signature Base string)
  • Signature Base String

问题
由于签名无效,我收到 HTTP(401 错误请求)返回。

注意:我很确定这是我构建签名基础字符串的方式。有关我使用的 API 文档的信息,请查看底部页面。

代码

GetOAuthToken(执行实际请求)

public static string GetAuthorizationToken()
{
string TimeInSecondsSince1970 = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
string Nonce = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(TimeInSecondsSince1970
+ TimeInSecondsSince1970 + TimeInSecondsSince1970));
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(GetAppleApiUrl.GetUrl(AppleApiUrl.SESSION_TOKEN));
httpWebRequest.Method = "GET";

string consumer_secret = Uri.EscapeDataString(Settings.SettingsManager.consumer_secret);
string token_secret = Uri.EscapeDataString(Settings.SettingsManager.access_secret);

string signature_base_string = GetSignatureBaseString(TimeInSecondsSince1970, Nonce);
string SHA1HASH = GetSha1Hash(consumer_secret + "&" + token_secret, signature_base_string);

string Header =
"OAuth realm=" + '"' + "ADM" + '"' + "," +
"oauth_consumer_key=" + '"' + Settings.SettingsManager.consumer_key + '"' + "," +
"oauth_token=" + '"' + Settings.SettingsManager.access_token + '"' + "," +
"oauth_signature_method=" + '"' + "HMAC-SHA1" + '"' + "," +
"oauth_signature= " + '"' + SHA1HASH + '"' + "," +
"oauth_timestamp=" + '"' + TimeInSecondsSince1970 + '"' + "," +
"oauth_nonce=" + '"' + Nonce + '"' + "," +
"oauth_version=" + '"' + "1.0" + '"' + ",";

httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, Header);
var Result = httpWebRequest.GetResponse();

return Result.ToString();
}

GetSha1Hash

public static string GetSha1Hash(string key, string message)
{
var encoding = new System.Text.ASCIIEncoding();

byte[] keyBytes = encoding.GetBytes(key);
byte[] messageBytes = encoding.GetBytes(message);

string Sha1Result = string.Empty;

using (HMACSHA1 SHA1 = new HMACSHA1(keyBytes))
{
var Hashed = SHA1.ComputeHash(messageBytes);
Sha1Result = Convert.ToBase64String(Hashed);
}

return Sha1Result;
}

GetSignatureBaseString

public static string GetSignatureBaseString(string TimeStamp, string Nonce)
{
//1.Convert the HTTP Method to uppercase and set the output string equal to this value.
string Signature_Base_String = "Get";
Signature_Base_String = Signature_Base_String.ToUpper();

//2.Append the ‘&’ character to the output string.
Signature_Base_String = Signature_Base_String + "&";

//3.Percent encode the URL and append it to the output string.
string PercentEncodedURL = Uri.EscapeDataString(GetAppleApiUrl.GetUrl(AppleApiUrl.SESSION_TOKEN));
Signature_Base_String = Signature_Base_String + PercentEncodedURL;

//4.Append the ‘&’ character to the output string.
Signature_Base_String = Signature_Base_String + "&";

//5.append parameter string to the output string.
Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("oauth_consumer_key=" + Settings.SettingsManager.consumer_key);
Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_token=" + Settings.SettingsManager.access_token);
Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_signature_method=" +"HMAC-SHA1");
Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_timestamp=" + TimeStamp);
Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_nonce=" + Nonce);
Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_version=" + "1.0");

return Signature_Base_String;
}

结果(Fiddler) Fiddler Result

API 文档 enter image description here

最佳答案

看起来您应该在 Header 字符串和 GetSignatureBaseString 方法中按字母顺序对参数进行排序,如 this comment 中所述和 Twitter OAuth documentation

关于c# - 使用 HMAC-SHA1 加密创建 OAuth 签名返回 HTTP 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com