【问题标题】:Twitter API returns 401 - unauthorizedTwitter API 返回 401 - 未经授权
【发布时间】:2016-12-24 02:51:54
【问题描述】:

我在使用 twitter API 时遇到问题。我想要实现的只是用户将能够通过我的网站上的 twitter 登录。

我正在使用这个库: Twitter

库适用于 google 和 facebook,但使用 twitter 我无法使其正常工作。

我收到了:

System.Net.WebException:远程服务器返回错误:(401) 未经授权。

我已经尝试过的:

在 twitter 开发者控制台上设置访问令牌并将此访问令牌粘贴到标题中。

我已按照this link on twitter 的指示进行操作 并创建了“授权”标头。它不起作用,异常仍然是 401 - 未授权。

我不明白的一件事是this link。

401错误的解释是:

身份验证凭据丢失或不正确。也退了 在其他情况下,例如现在对 API v1 端点的所有调用 返回 401(改用 API v1.1)。

因此,对 API v1 的所有调用都将返回 401。“改用 API v1.1” - 如何执行此操作?使用 v1.1 而不是 v1 的设置在哪里?

这是来自原始代码的方法,经过我的修改 - 添加“授权”标头:

namespace Oauth2Login.Core
{
    public class RestfullRequest
    {
        public static string Request(string url, string method, string contentType, NameValueCollection authorizationHeader,
            string data, string proxyAddress = null)
        {
            var request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = method;
            if (!string.IsNullOrEmpty(contentType))
                request.ContentType = contentType;

            string authHeaderString = "OAuth ";
            authHeaderString += PercentEncode("oauth_consumer_key") + "=\"" + (authorizationHeader.GetValues("oauth_consumer_key"))[0] + "\", ";
            authHeaderString += PercentEncode("oauth_nonce") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_nonce"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_signature") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_signature"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_signature_method") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_signature_method"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_timestamp") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_timestamp"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_token") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_token"))[0]) + "\", ";
            authHeaderString += PercentEncode("oauth_version") + "=\"" + PercentEncode((authorizationHeader.GetValues("oauth_version"))[0]) + "\"";

            request.Headers.Add("Authorization", authHeaderString);

            if (!string.IsNullOrEmpty(proxyAddress))
            {
                IWebProxy proxy = new WebProxy(proxyAddress);
                proxy.Credentials = new NetworkCredential();
                request.Proxy = proxy;
            }

            if (!string.IsNullOrEmpty(data))
            {
                using (var swt = new StreamWriter(request.GetRequestStream()))
                {
                    swt.Write(data);
                }
            }

            string result = string.Empty;

            /*this line throws 401 - not authorized exception*/
            using (WebResponse response = request.GetResponse())
            {
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                }
            }
            return result;
        }

        public static string PercentEncode(string source)
        {
            return source
                .Replace(" ", "%20").Replace("!", "%21").Replace("&", "%26")
                .Replace(@"/", "%2F").Replace("=", "%3D").Replace("+", "%2B")
                .Replace(",", "%2C").Replace("-", "%2D").Replace(".", "%2E");
        }        
    }
}

【问题讨论】:

    标签: c# .net twitter oauth


    【解决方案1】:

    也许这对你有帮助:

    How to use cursors to iterate through the result set of GET followers/list in Twitter API using c#?

    检查该部分,直到私有 void StartCreateCall() 方法中的注释“//现在我们已被授予访问权限...”。

    【讨论】:

      猜你喜欢
      • 2014-01-02
      • 1970-01-01
      • 2012-06-24
      • 2012-04-08
      • 1970-01-01
      • 2023-03-09
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多