0

我需要为移动应用程序进行 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 签名是错误的。但我看过,我什么也没看到。

4

1 回答 1

0

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

于 2014-08-13T11:29:39.017 回答