Twitter API 在调用 account/verify_credentials 时返回 215 错误的身份验证数据。json
Twitter API returning 215 Bad Authentication Data when calling account/verify_credentials.json
我正在编写一个 API,它需要验证 Twitter SDK 在移动设备上创建的 OAuth 令牌和令牌机密。
为此,我试图调用 account/verify_credentials.json of the Twitter API. I have jumped through all the OAuth signature generation hoops,但在每次请求时,我都会得到相同的响应:
{
"errors": [{
"code": 215,
"message": "Bad Authentication data."
}]
}
我无法解释为什么会发生这种情况,因为我遵循了 Twitter 的指导方针并参考了其他几个具有类似目标的问题,但仍然没有运气。
另外,请注意我不想为此使用库。我需要做的就是这一个请求,所以额外的库会不必要地膨胀。下面是我生成错误响应的代码:
var httpMethod = HttpMethod.Get;
var uri = new Uri("https://api.twitter.com/1.1/account/verify_credentials.json");
var nonce = Guid.NewGuid().ToString();
var timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(CultureInfo.InvariantCulture);
var oauthParameters = new SortedDictionary<string, string>
{
{"oauth_consumer_key", Settings.OAuth.Twitter.ConsumerKey},
{"oauth_nonce", nonce},
{"oauth_signature_method", "HMAC-SHA1"},
{"oauth_timestamp", timestamp},
{"oauth_token", accessToken},
{"oauth_version", "1.0"}
};
var signingParameters = new SortedDictionary<string, string>(oauthParameters);
var parsedQuery = HttpUtility.ParseQueryString(uri.Query);
foreach (var k in parsedQuery.AllKeys)
signingParameters.Add(k, parsedQuery[k]);
var parameterString = string.Join("&", signingParameters.Keys.Select(k => $"{Uri.EscapeDataString(k)}={Uri.EscapeDataString(signingParameters[k])}"));
var stringToSign = string.Join("&", new[] { httpMethod.Method.ToUpper(), uri.AbsoluteUri, parameterString }.Select(Uri.EscapeDataString));
var signingKey = Settings.OAuth.Twitter.ConsumerSecret + "&" + tokenSecret;
var signature = Convert.ToBase64String(new HMACSHA1(Encoding.ASCII.GetBytes(signingKey)).ComputeHash(Encoding.ASCII.GetBytes(stringToSign)));
oauthParameters.Add("oauth_signature", signature);
var authHeader = string.Join(",", oauthParameters.Keys.Select(k => $"{Uri.EscapeDataString(k)}=\"{Uri.EscapeDataString(oauthParameters[k])}\""));
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);
var response = await client.GetAsync(uri);
return await response.Content.ReadAsStringAsync(); // always error 215
我刚刚逐字逐句地尝试了您的代码,效果非常好。
您的问题一定是您的 Twitter 应用程序密钥。再次尝试从 apps.twitter.com 复制它们。如果仍然不起作用,您可能需要重新创建密钥。
我正在编写一个 API,它需要验证 Twitter SDK 在移动设备上创建的 OAuth 令牌和令牌机密。
为此,我试图调用 account/verify_credentials.json of the Twitter API. I have jumped through all the OAuth signature generation hoops,但在每次请求时,我都会得到相同的响应:
{
"errors": [{
"code": 215,
"message": "Bad Authentication data."
}]
}
我无法解释为什么会发生这种情况,因为我遵循了 Twitter 的指导方针并参考了其他几个具有类似目标的问题,但仍然没有运气。
另外,请注意我不想为此使用库。我需要做的就是这一个请求,所以额外的库会不必要地膨胀。下面是我生成错误响应的代码:
var httpMethod = HttpMethod.Get;
var uri = new Uri("https://api.twitter.com/1.1/account/verify_credentials.json");
var nonce = Guid.NewGuid().ToString();
var timestamp = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(CultureInfo.InvariantCulture);
var oauthParameters = new SortedDictionary<string, string>
{
{"oauth_consumer_key", Settings.OAuth.Twitter.ConsumerKey},
{"oauth_nonce", nonce},
{"oauth_signature_method", "HMAC-SHA1"},
{"oauth_timestamp", timestamp},
{"oauth_token", accessToken},
{"oauth_version", "1.0"}
};
var signingParameters = new SortedDictionary<string, string>(oauthParameters);
var parsedQuery = HttpUtility.ParseQueryString(uri.Query);
foreach (var k in parsedQuery.AllKeys)
signingParameters.Add(k, parsedQuery[k]);
var parameterString = string.Join("&", signingParameters.Keys.Select(k => $"{Uri.EscapeDataString(k)}={Uri.EscapeDataString(signingParameters[k])}"));
var stringToSign = string.Join("&", new[] { httpMethod.Method.ToUpper(), uri.AbsoluteUri, parameterString }.Select(Uri.EscapeDataString));
var signingKey = Settings.OAuth.Twitter.ConsumerSecret + "&" + tokenSecret;
var signature = Convert.ToBase64String(new HMACSHA1(Encoding.ASCII.GetBytes(signingKey)).ComputeHash(Encoding.ASCII.GetBytes(stringToSign)));
oauthParameters.Add("oauth_signature", signature);
var authHeader = string.Join(",", oauthParameters.Keys.Select(k => $"{Uri.EscapeDataString(k)}=\"{Uri.EscapeDataString(oauthParameters[k])}\""));
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", authHeader);
var response = await client.GetAsync(uri);
return await response.Content.ReadAsStringAsync(); // always error 215
我刚刚逐字逐句地尝试了您的代码,效果非常好。
您的问题一定是您的 Twitter 应用程序密钥。再次尝试从 apps.twitter.com 复制它们。如果仍然不起作用,您可能需要重新创建密钥。