我在从 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;
}
}
新版本中的标题略有不同,但保持相同并没有帮助。
感谢您的任何建议!