Twitter API:401上的POST,GET工作正常

时间:2012-08-04 19:22:18

标签: c# httpwebrequest twitter twitter-oauth http-status-code-401

我已经搜索了几天,高低,并且无法获得POST API以正常工作。我能够成功获取请求令牌并将其交换为访问令牌,但是当我对状态/更新进行POST时,我收到401错误。

我已经确认标题的授权部分与我在使用Twitter OAuth工具时收到的内容完全匹配。

我认为我的问题是我将状态添加到Web请求的正文中。该网址是https://api.twitter.com/1/statuses/update.json,还是https://api.twitter.com/1/statuses/update.json?status=MyTweetHere

我的代码如下:

        public string UpdateStatus(string requestMethod, string baseUrl, string consumerKey, string consumerSecret, string accessToken, OAuthRequestType requestType, string status)
    {
        //baseUrl = https://api.twitter.com/1/statuses/update.json
        const string signatureMethod = "HMAC-SHA1";
        const string oauthVersion = "1.0";

        var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
        var timestamp =  (int)timeSpan.TotalSeconds;

        var nonce = CreateNOnce(timestamp, consumerKey, baseUrl, accessToken);
        var signature = CreateSignature(requestMethod, baseUrl, consumerKey, consumerSecret, nonce, signatureMethod, timestamp, accessToken, oauthVersion, string.Empty, string.Empty, requestType, status);

        var bodyData = "status=" + UrlEncode(status);

        var webRequest = (HttpWebRequest)WebRequest.Create(baseUrl);
        webRequest.KeepAlive = true;
        webRequest.ServicePoint.Expect100Continue = false;
        webRequest.Method = requestMethod; //Send in as POST
        webRequest.Accept = "*/*";
        webRequest.UserAgent = "Renicorp.Social";

        webRequest.Headers["Authorization"] = "OAuth " + UrlEncode("oauth_consumer_key") + "=\"" + UrlEncode(consumerKey) + "\", " +
                                                         UrlEncode("oauth_nonce") + "=\"" + UrlEncode(nonce) + "\", " +
                                                         UrlEncode("oauth_signature") + "=\"" + UrlEncode(signature) + "\", " +
                                                         UrlEncode("oauth_signature_method") + "=\"" + UrlEncode(signatureMethod) + "\", " +
                                                         UrlEncode("oauth_timestamp") + "=\"" + UrlEncode(timestamp.ToString()) + "\", " +
                                                         UrlEncode("oauth_token") + "=\"" + UrlEncode(accessToken) + "\", " +
                                                         UrlEncode("oauth_version") + "=\"" + UrlEncode(oauthVersion) + "\"";

        string postData = bodyData;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.ContentLength = byteArray.Length;

        using (Stream dataStream = webRequest.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        //Make the web call and return the response
        WebResponse response = webRequest.GetResponse();
        var responseBody = string.Empty;
        Stream stream = response.GetResponseStream();
        if (stream != null) responseBody = new StreamReader(stream).ReadToEnd();
        return responseBody;
    }

更新 我现在已经编写了一个方法来为account / verify_credentials.json执行GET,并且如果我使用我的应用程序帐户密钥,则能够验证帐户凭据,但是当我尝试使用单独的帐户并提供访问令牌时,我得到401。

1 个答案:

答案 0 :(得分:1)

所以我的问题是创建签名密钥时的密钥。我在twitter上的应用程序中硬编码了我的密钥。这就是我的标题始终匹配的原因,但是当我使用链接的用户帐户运行测试时,请求将不会通过。故事的道德不是硬编码密钥。我一直想知道秘密密钥的用途。现在我知道了。

此外,要使用的网址只是基本网址:https://api.twitter.com/1/statuses/update.json。无需添加url参数。最后,在创建http请求主体时,它只是“status = UrlEncode(tweetToBeMade)”。