Twitter OAuth request_token停止工作

时间:2015-06-25 14:17:55

标签: c# asp.net twitter oauth twitter-oauth

我的twitter API调用,特别是我在3-legged auth中的第一步,停止了工作。我已经将时间戳,密钥和所有内容与OAuth签名生成器工具进行了比较,它们都匹配(execpt oauth_nonce,但这就是我猜的那一点)。这是我的代码。任何建议或小的意见将不胜感激。

protected void RequestToken()
{
        string oauthcallback = Request.Url.Host + "/TwitterCallback.aspx";
        string oauthconsumerkey = "xxx-consumerkey";
        string oauthconsumersecret = "xxx-consumerSecret";
        string oauthtokensecret = string.Empty;
        string oauthtoken = string.Empty;
        string oauthsignaturemethod = "HMAC-SHA1";
        string oauthversion = "1.0";
        string oauthnonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        string oauthtimestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
        string url = "https://api.twitter.com/oauth/request_token?oauth_callback=" + oauthcallback;
        SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>();
        basestringParameters.Add("oauth_version", oauthversion);
        basestringParameters.Add("oauth_consumer_key", oauthconsumerkey);
        basestringParameters.Add("oauth_nonce", oauthnonce);
        basestringParameters.Add("oauth_signature_method", oauthsignaturemethod);
        basestringParameters.Add("oauth_timestamp", oauthtimestamp);
        basestringParameters.Add("oauth_callback", Uri.EscapeDataString(oauthcallback));

        //Build the signature string
        string baseString = String.Empty;
        baseString += "POST" + "&";
        baseString += Uri.EscapeDataString(url.Split('?')[0]) + "&";
        foreach (KeyValuePair<string, string> entry in basestringParameters)
        {
            baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
        }

        //Remove the trailing ambersand char last 3 chars - %26
        //baseString = baseString.Substring(0, baseString.Length - 3);

        //Build the signing key
        string signingKey = Uri.EscapeDataString(oauthconsumersecret) +
          "&" + Uri.EscapeDataString(oauthtokensecret);

        //Sign the request
        HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
        string oauthsignature = Convert.ToBase64String(
          hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));

        //Tell Twitter we don't do the 100 continue thing
        ServicePointManager.Expect100Continue = false;
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@url);

        string authorizationHeaderParams = String.Empty;
        authorizationHeaderParams += "OAuth ";
        authorizationHeaderParams += "oauth_nonce=" + "\"" +
          Uri.EscapeDataString(oauthnonce) + "\",";
        authorizationHeaderParams += "oauth_signature_method=" + "\"" +
          Uri.EscapeDataString(oauthsignaturemethod) + "\",";
        authorizationHeaderParams += "oauth_timestamp=" + "\"" +
          Uri.EscapeDataString(oauthtimestamp) + "\",";
        authorizationHeaderParams += "oauth_consumer_key=" + "\"" +
          Uri.EscapeDataString(oauthconsumerkey) + "\",";
        authorizationHeaderParams += "oauth_signature=" + "\"" +
          Uri.EscapeDataString(oauthsignature) + "\",";
        authorizationHeaderParams += "oauth_version=" + "\"" +
          Uri.EscapeDataString(oauthversion) + "\"";
        webRequest.Headers.Add("Authorization", authorizationHeaderParams);

        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        //Allow us a reasonable timeout in case Twitter's busy
        webRequest.Timeout = 3 * 60 * 1000;

        try
        {
            HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;
            Stream dataStream = webResponse.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            var uri = new Uri("https://test.dk?" + responseFromServer);
            var token = HttpUtility.ParseQueryString(uri.Query).Get("oauth_token"); ;
            var tokensecret = HttpUtility.ParseQueryString(uri.Query).Get("oauth_token_secret");
            Response.Write(responseFromServer);
            Response.Redirect("https://api.twitter.com/oauth/authorize?force_login=true&oauth_token=" + token);
        }
        catch (Exception ex)
        {
            Response.Write(ex.GetBaseException());
        }


}

当我执行HTTP请求webRequest.GetResponse()

时,显然会发生错误

返回 401未经授权

1 个答案:

答案 0 :(得分:0)

您现在必须在URL中包含oauth版本号,否则它将回退到最旧版本(或者可能是最新版本,不记得了。)

提醒/oath/1.0//1.0/oauth/或解决我的问题,因为我记得它(已经有一段时间了)。