Twitter请求令牌返回401未授权。我找不到我做错了什么

时间:2013-08-26 09:23:06

标签: c#-4.0 twitter oauth

我需要为移动应用程序进行Twitter登录。到目前为止,我已经尝试了很多库,包括tweetsharp和linq2twitter。但我无处可去。由于所有这些麻烦,我使用了webclient电话,但我再次陷入困境。当我拨打电话时,我收到401 Unauthorized错误。我在这里包含我的代码。你们可以看到任何错误吗?我真的很困惑。

        string oauth_signature_method = "HMAC-SHA1";
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();

        string oauth_version = "1.0";
        string oauth_consumer_key = "iD1232134AQ2Pb6Q";
        string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));

        SortedDictionary<string, string> sd = new SortedDictionary<string, string>();

        sd.Add("oauth_version", oauth_version);
        sd.Add("oauth_consumer_key", oauth_consumer_key);
        sd.Add("oauth_nonce", oauth_nonce);
        sd.Add("oauth_signature_method", oauth_signature_method);
        sd.Add("oauth_timestamp", oauth_timestamp);
        UrlEntity callback = new UrlEntity();
        callback.Url = @"//tweeter";
        string encodedCallbackUrl = HttpUtility.UrlEncode(callback.Url);
        sd.Add("oauth_callback",encodedCallbackUrl);

        WebClient wc = new WebClient();
        wc.Headers.Add("User-Agent: randomAgent HTTP Client");
        wc.Headers.Add("Host: api.twitter.com");
        wc.Headers.Add(@"Accept: */*");
        UrlEntity url = new UrlEntity();
        url.Url = @"https://api.twitter.com/oauth/request_token";
        string signature = CreateSignature(url, sd);
        sd.Add("oauth_signature",signature);
        string dataValues = "";
        foreach (KeyValuePair<string, string> pair in sd)
        {
            dataValues += pair.Key + "=''" + pair.Value + "'',";
        }
        dataValues = dataValues.Substring(0, dataValues.Length - 1); // cuts off the last,
        string headerVal = " Oauth " + dataValues;
        wc.Headers.Add("Authorization",headerVal);
        wc.UploadString(@"https://api.twitter.com/oauth/request_token", "");
        wc.DownloadStringCompleted += WcOnDownloadStringCompleted;

以下是签名的代码。

public static string CreateSignature(UrlEntity url, SortedDictionary<string, string> sd)
    {
        string parameterString = "";
        SortedDictionary<string, string> sd2 = new SortedDictionary<string, string>();
        foreach (KeyValuePair<string, string> parameter in sd)
        {
            string encodedKey = HttpUtility.UrlEncode(parameter.Key);
            string encodedvalue = HttpUtility.UrlEncode(parameter.Value);
            sd2.Add(encodedKey,encodedvalue);
        }

        foreach (KeyValuePair<string, string> pair in sd2)
        {
            parameterString += pair.Key + "=" + pair.Value + "&";
        }

        parameterString = parameterString.Substring(0, parameterString.Length - 1); // cuts off the last &


        string signatureString = "POST&";
        var encodedUrl = HttpUtility.UrlEncode(url.Url); // percent encode URL 
        signatureString += encodedUrl + "&";
        signatureString += HttpUtility.UrlEncode(parameterString); // percent encode entire parameter string

        string consumerSecret = "supercorectConsumersecretfromtwitterwebsite";
        string signingKey = HttpUtility.UrlEncode(consumerSecret) + "&";
        HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));

        string signatureStringFinal = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(signatureString)));


        return signatureStringFinal;
    }

有点乱,但我被卡住了。以下链接可能会有所帮助。     https://dev.twitter.com/docs/auth/implementing-sign-twitter     https://dev.twitter.com/docs/api/1/post/oauth/request_token     https://dev.twitter.com/docs/auth/creating-signature

编辑:

我看过使用fiddler的请求。 响应消息中有这个...

“无法验证oauth签名和令牌”

这可能意味着oauth签名是错误的。但我看了,我什么都没看到。

1 个答案:

答案 0 :(得分:0)

我不是.net专家,但我正在为PHP设置类似内容,并注意Oauth属性必须按字母顺序排列。从快速浏览看,这里的例子似乎并非如此。请参阅https://dev.twitter.com/docs/auth/creating-signature