简体   繁体   English

Twitter API集成-远程服务器返回错误:(401)未经授权

[英]Twitter API Integration - The remote server returned an error: (401) Unauthorized

I have tried to integrate twitter API Integration but no luck there is getting error 我已经尝试集成twitter API集成,但是没有运气,会出现错误

"The remote server returned an error: (401) Unauthorized. " “远程服务器返回错误:(401)未经授权。”

Could you please help me as soon as possible? 您能尽快帮助我吗?

My Twitter Application details below 我的Twitter应用程序详细信息如下

Access level    Read-only 
About the application permission model

Consumer key    <Consumer Key>
Consumer secret <Consumer Secret>
Request token URL   https://api.twitter.com/oauth/request_token
Authorize URL   https://api.twitter.com/oauth/authorize
Access token URL    https://api.twitter.com/oauth/access_token
Callback URL    http://www.opalevents.org/register
Sign in with Twitter    Yes

My Code below 我的以下代码

![/ oauth application keys
            var oauth_token = "https://api.twitter.com/oauth/request_token";
            var oauth_token_secret = "https://api.twitter.com/oauth/access_token";
            var oauth_consumer_key = "<Consumer Key>";
            var oauth_consumer_secret = "<Consumer Secret>";

            // oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // unique request details
            var oauth_nonce = Convert.ToBase64String(
                new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            // message api details
            var status = "Updating status via REST API if this works";
            var resource_url = "http://api.twitter.com/1/statuses/update.json";

            // create oauth signature
            var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";

            var baseString = string.Format(baseFormat,
                                        oauth_consumer_key,
                                        oauth_nonce,
                                        oauth_signature_method,
                                        oauth_timestamp,
                                        oauth_token,
                                        oauth_version,
                                        Uri.EscapeDataString(status)
                                        );

            baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                    "&", Uri.EscapeDataString(oauth_token_secret));

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                               "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                               "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                                    Uri.EscapeDataString(oauth_nonce),
                                    Uri.EscapeDataString(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_version)
                            );


            // make the request
            var postBody = "status=" + Uri.EscapeDataString(status);

            ServicePointManager.Expect100Continue = false;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            using (Stream stream = request.GetRequestStream())
            {
                byte\[\] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);
            }
            WebResponse response = request.GetResponse();][1]

First, your resource url is wrong, it should be: https://stream.twitter.com/1.1/statuses/filter.json 首先,您的资源网址错误,应该是: https : //stream.twitter.com/1.1/statuses/filter.json

Notice that there is a new version 1.1 . 注意,有一个新的版本1.1。 You can also try to go to the OAuth tool tab and regenerate a new request like this: 您也可以尝试转到OAuth工具标签并重新生成一个新请求,如下所示:

OAuth令牌

From time to time I have to regenerate my credentials because it kicks me out 有时我必须重新生成我的凭据,因为它会将我赶出场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Twitter搜索API远程服务器返回错误:(401)未经授权 - Twitter Search API The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401) Unauthorized。 Twitter oAuth - The remote server returned an error: (401) Unauthorized. Twitter oAuth Box API返回错误::远程服务器返回错误:(401)未经授权 - Box API returned Error : : The remote server returned an error: (401) Unauthorized REST Search API远程服务器返回错误:(401)未经授权 - REST Search API The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未授权的sharepoint - The remote server returned an error: (401) Unauthorized sharepoint EWS:“远程服务器返回错误(401)未经授权” - EWS: “The remote server returned error (401) Unauthorized” 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized Web 异常:远程服务器返回错误 (401) 未授权 - Webexception:remote server returned an error (401)unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM