为什么我调用来获取用户的Twitter时间线返回" 401未授权&QUOT ;?。 [英] Why is my call to get Twitter user timeline returning "401 Unauthorized."?

查看:156
本文介绍了为什么我调用来获取用户的Twitter时间线返回" 401未授权&QUOT ;?。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码无耻地从 dev.twitter 采取以获得特定用户的时间线:

I have the following code shamelessly taken from dev.twitter to get the time line of a specific user:

string endpoint = "https://api.twitter.com/1.1/statuses/user_timeline.json";

string user_name = ".....";

string ConsumerKey = "....";
string ConsumerSecret = "........";
string Token = ".......";
string TokenSecret = "........";

// oauth application keys
var oauth_token = Token;
var oauth_token_secret = TokenSecret;
var oauth_consumer_key = ConsumerKey;
var oauth_consumer_secret = ConsumerSecret;

// oauth implementation details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";

// unique request details
var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
    - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";

var baseString = string.Format(baseFormat,
                            oauth_consumer_key,
                            oauth_nonce,
                            oauth_signature_method,
                            oauth_timestamp,
                            oauth_token,
                            oauth_version,
                            user_name
                            );

//&include_rts=true &exclude_replies=false

baseString = string.Concat("GET&", Uri.EscapeDataString(endpoint), "&", Uri.EscapeDataString(baseString));

var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                        "&", Uri.EscapeDataString(oauth_token_secret));

string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
    oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}

string headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                      "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                      "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                      "oauth_version=\"{6}\",screen_name=\"{7}\"";
var authHeader = string.Format(headerFormat,
                               Uri.EscapeDataString(oauth_nonce),
                               Uri.EscapeDataString(oauth_signature_method),
                               Uri.EscapeDataString(oauth_timestamp),
                               Uri.EscapeDataString(oauth_consumer_key),
                               Uri.EscapeDataString(oauth_token),
                               Uri.EscapeDataString(oauth_signature),
                               Uri.EscapeDataString(oauth_version),
                               Uri.EscapeDataString(user_name)
            );

// make the request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var objText = reader.ReadToEnd();



不过,我得到一个401,在这条线未经授权的错误:

However, I'm getting a 401, Unauthorized error on this line:

var response = (HttpWebResponse)request.GetResponse();



(如确有上Twitter的开发者线程近一海报)。

(as indeed are the last couple of posters on that Twitter developer thread).

有没有什么办法可以快速检查我的各种令牌,密钥和秘密是有效的,应该返回我要找的?

Is there any way I can quickly check that my various tokens, keys and secrets are valid and should return the data I'm looking for?

推荐答案

我创建了这个位置确实在GitHub上一个测试控制台应用程序:

I have created a test console application on GitHub that does this here:

Authenticate并请求用户的时间表与Twitter的API 1.1 OAuth的

请看看我的问题和答案,标记,如果你发现使用它。

Please see my question and answer, markup if you find it of use.

这篇关于为什么我调用来获取用户的Twitter时间线返回" 401未授权&QUOT ;?。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆