我正在使用以下代码来尝试获取 oauth_token 和 oauth_secret。
string url = "https://api.twitter.com/oauth/request_token";
string base_format = "oauth_callback={0}&oauth_consumer_key={1}&oauth_nonce={2}&oauth_signature_method={3}&oauth_timestamp={4}&oauth_version={5}";
string base_string = String.Format(base_format, oauth_callback, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_version);
base_string = String.Concat("POST&", Uri.EscapeDataString(url), Uri.EscapeDataString(base_string));
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
"&");
string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(base_string)));
}
string header_format = "OAuth oauth_nonce=\"{0}\", oauth_callback=\"{1}\", oauth_signature_method=\"{2}\", oauth_timestamp=\"{3}\", oauth_consumer_key=\"{4}\", oauth_signature=\"{5}\", oauth_version=\"{6}\"";
string header = string.Format(header_format, Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_callback), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauth_consumer_key), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_version));
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Headers.Add("Authorization", header);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
WebResponse response = req.GetResponse();
它还给我一个 401。我看不出这里有什么问题。需要帮助。