Twitter API返回401 - 未经授权

时间:2016-08-17 12:32:07

标签: c# .net twitter oauth

我遇到twitter API问题。我想要实现的只是用户可以通过我的网站上的Twitter登录。

我正在使用这个库: Twitter

图书馆适用于谷歌和Facebook,但有了推特,我无法使其发挥作用。

我收到了:

  

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");
        }        
    }
}

1 个答案:

答案 0 :(得分:0)

也许这可能会对你有所帮助:

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

在私有void StartCreateCall()方法中检查部分,直到评论'//现在我们已被授予访问权限...'。