C#推特和Restsharp身份验证
本文关键字:Restsharp 身份验证 | 更新日期: 2023-09-27 18:21:05
我在C#中创建了一个请求,以便在Twitter中检索用户主时间线,但我收到了{“message”:“Could not authenticate you”,“code”:32}
错误。以下是我尝试访问Twitter REST API的方法:
var stringBuilder = new StringBuilder();
stringBuilder.Append("GET&");
stringBuilder.Append(Uri.EscapeDataString(baseString));
stringBuilder.Append("&");
//the key value pairs have to be sorted by encoded key
var sd = new SortedDictionary<string, string> ();
sd.Add("oauth_version", "1.0");
sd.Add ("oauth_consumer_key", twitterOauthConsumerKey);
sd.Add("oauth_nonce", twitterOauthNonce);
sd.Add("oauth_signature_method", "HMAC-SHA1");
sd.Add("oauth_timestamp", twitterOauthTimestamp);
sd.Add("oauth_token", twitterOauthToken);
//GS - Build the signature string
foreach (var keyValuePair in sd)
{
//append a = between the key and the value and a & after the value
stringBuilder.Append(Uri.EscapeDataString(string.Format("{0}={1}&", keyValuePair.Key, keyValuePair.Value)));
}
string signatureBaseString = stringBuilder.ToString().Substring(0, stringBuilder.Length - 3);
//generation the signature key the hash will use
string signatureKey =
Uri.EscapeDataString(twitterOauthConsumerKey) + "&" +
Uri.EscapeDataString(twitterOauthToken);
var hmacsha1 = new HMACSHA1(
new ASCIIEncoding().GetBytes(signatureKey));
//hash the values
string signatureString = Convert.ToBase64String(
hmacsha1.ComputeHash(
new ASCIIEncoding().GetBytes(signatureBaseString)));
Console.WriteLine (signatureString);
var request = new RestRequest("statuses/home_timeline.json", Method.GET);
request.AddParameter ("oauth_consumer_key", twitterOauthConsumerKey);
request.AddParameter ("oauth_signature_method", "HMAC-SHA1");
request.AddParameter ("oauth_timestamp", twitterOauthTimestamp);
request.AddParameter ("oauth_nonce", twitterOauthNonce);
request.AddParameter ("oauth_version", "1.0");
request.AddParameter ("oauth_token", twitterOauthToken);
request.AddParameter ("oauth_signature", signatureString);
var client = new RestClient ("https://api.twitter.com/1.1");
client.ExecuteAsync (request, response => {
Console.WriteLine(response.Content);
});
我觉得我创建了一个正确的签名字符串。并且用户已完全通过身份验证。应用程序权限为"读+写"权限。
var twitterOauthToken = twitter.Properties ["oauth_token"].ToString ();
var twitterOauthTokenSecret = twitter.Properties ["oauth_token_secret"].ToString ();
var twitterOauthConsumerKey = twitter.Properties ["oauth_consumer_key"].ToString ();
var twitterOauthConsumerSecret = twitter.Properties ["oauth_consumer_secret"].ToString ();
var request = new RestRequest("1.1/statuses/user_timeline.json");
var client = new RestClient("https://api.twitter.com")
{
Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource(twitterOauthConsumerKey, twitterOauthConsumerSecret, twitterOauthToken, twitterOauthTokenSecret)
};
client.ExecuteAsync (request, response => {
Console.WriteLine(response.Content);
var rootObject = JsonConvert.DeserializeObject<Floadt.Core.Twitter.RootObject> (response.Content);
//((TableSource)table.Source).facebookData = rootObject;
});
简单的解决方案我想得太多了