我在异步地从Twitter获取请求令牌时遇到问题 - 服务器返回"远程服务器返回错误:(401)未经授权。"
这是我使用的代码 -
public void AcquireRequestToken(Action<bool> response)
{
string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
// build the signature
var headers = new Dictionary<string,string>()
{
{ "oauth_consumer_key", _oAuthConfig.ConsumerKey },
{ "oauth_nonce", oauth_nonce },
{ "oauth_signature_method", "HMAC-SHA1" },
{ "oauth_timestamp", MakeTimestamp() },
{ "oauth_version", "1.0" },
{ "oauth_callback", PercentEncode(_oAuthConfig.Callback) },
};
string signature = MakeSignature ("POST", _oAuthConfig.RequestTokenUrl, headers);
string compositeSigningKey = MakeSigningKey(_oAuthConfig.ConsumerSecret, null);
string oauth_signature = MakeOAuthSignature(compositeSigningKey, signature);
Uri fullUri = new Uri(_oAuthConfig.RequestTokenUrl);
var request = (HttpWebRequest)WebRequest.Create(fullUri);
request.Method = "POST";
request.Headers.Add("oauth_consumer_key", PercentEncode(_oAuthConfig.ConsumerKey));
request.Headers.Add("oauth_nonce", PercentEncode(oauth_nonce));
request.Headers.Add("oauth_signature_method", PercentEncode("HMAC-SHA1"));
request.Headers.Add("oauth_timestamp", PercentEncode(MakeTimestamp()));
request.Headers.Add("oauth_version", "1.0");
request.Headers.Add("oauth_callback", PercentEncode(_oAuthConfig.Callback));
request.Headers.Add("oauth_signature", PercentEncode(oauth_signature));
try
{
request.BeginGetResponse(new AsyncCallback(result =>
{
string contents = String.Empty;
HttpWebRequest theRequest = (HttpWebRequest)result.AsyncState;
if (theRequest != null)
{
try
{
HttpWebResponse theResponse = (HttpWebResponse)theRequest.EndGetResponse(result);
using (Stream stream = theResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
contents = reader.ReadToEnd();
}
Dictionary<string, object> results = JsonConvert.DeserializeObject<Dictionary<string, object>>(contents);
_requestToken = (string)results ["oauth_token"];
_requestTokenSecret = (string)results ["oauth_token_secret"];
response(true);
}
catch (WebException e)
{
response(false);
}
}
else
{
response(false);
}
}), request);
}
catch (WebException e)
{
response(false);
}
}
我的旧(非异步)代码运行正常 -
public bool AcquireRequestToken()
{
var headers = new Dictionary<string,string>()
{
{ "oauth_callback", PercentEncode(_oAuthConfig.Callback) },
{ "oauth_consumer_key", _oAuthConfig.ConsumerKey },
{ "oauth_signature_method", "HMAC-SHA1" },
{ "oauth_timestamp", MakeTimestamp() },
{ "oauth_version", "1.0" }
};
string signature = MakeSignature ("POST", _oAuthConfig.RequestTokenUrl, headers);
string compositeSigningKey = MakeSigningKey(_oAuthConfig.ConsumerSecret, null);
string oauth_signature = MakeOAuthSignature(compositeSigningKey, signature);
var wc = new WebClient ();
headers.Add ("oauth_signature", PercentEncode(oauth_signature));
wc.Headers [HttpRequestHeader.Authorization] = HeadersToOAuth(headers);
try
{
var result = HttpUtility.ParseQueryString(wc.UploadString (new Uri(_oAuthConfig.RequestTokenUrl), ""));
if (result ["oauth_callback_confirmed"] != null)
{
_requestToken = result ["oauth_token"];
_requestTokenSecret = result ["oauth_token_secret"];
return true;
}
}
catch (Exception e)
{
return false;
}
}
标题在新版本中略有不同,但保持相同并不起作用。
感谢您的任何建议!
答案 0 :(得分:0)
标题错了。只有一个名为Authorization
的HTTP标头:
Authorization: OAuth oauth_consumer_key="<the consumer key of your app>", oauth_nonce="<the nonce>", oauth_signature="<the signature>", oauth_signature_method="HMAC-SHA1", oauth_timestamp="<the timestamp>", oauth_token="<your token>", oauth_version="1.0"
此页面将告诉您如何制作它:https://dev.twitter.com/docs/auth/authorizing-request。除了两种方法外,所有方法都是一样的:
request_token
需要oauth_callback
,但不需要oauth_token
(正常,因为您没有)。access_token
需要oauth_verifier
(您将在第二步后获得)。