[英]Twitter Search API The remote server returned an error: (401) Unauthorized
I have an app trying to make a simple GET call to a Twitter feed, but it;s failing with the error above. 我有一个应用程序试图对Twitter feed进行简单的GET调用,但是由于上面的错误而失败。 I am able to successfully get an access_token before calling the code below.
在调用下面的代码之前,我能够成功获取一个access_token。
string lUrl = "https://api.twitter.com/1.1/search/tweets.json&q=" + HttpUtility.UrlEncode("@typescriptlang");
var headerFormat = "Bearer {0}";
var authHeader = string.Format(
headerFormat,
Convert.ToBase64String(Encoding.ASCII.GetBytes(HttpUtility.UrlEncode(access_token)))
);
HttpWebRequest lRequest = HttpWebRequest.CreateHttp(lUrl);
lRequest.Method = "GET";
lRequest.Headers.Add("Authorization", authHeader);
WebResponse lResponse = lRequest.GetResponse();
Ok, this is exactly what you need to do, I have tested this and it is a working solution: 好的,这正是您需要做的,我已经测试过了,这是一个可行的解决方案:
private string oAuthConsumerKey = ConfigurationManager.AppSettings["oAuthConsumerKey"];
private string oAuthConsumerSecret = ConfigurationManager.AppSettings["oAuthConsumerSecret"];
private string oAuthUrl = ConfigurationManager.AppSettings["oAuthUrl"];
private static string searchFormat = ConfigurationManager.AppSettings["searchFormat"];
private string searchUrl = string.Format(searchFormat, "%23test");
public string GetSearch()
{
// Do the Authenticate
var authHeaderFormat = "Basic {0}";
var authHeader = string.Format(authHeaderFormat,
Convert.ToBase64String(
Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
Uri.EscapeDataString((oAuthConsumerSecret)))
));
var postBody = "grant_type=client_credentials";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (Stream stream = authRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
stream.Write(content, 0, content.Length);
}
authRequest.Headers.Add("Accept-Encoding", "gzip");
WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
TwitAuthenticateResponse twitAuthResponse;
using (authResponse)
{
using (var reader = new StreamReader(authResponse.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objectText = reader.ReadToEnd();
twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
}
}
// Do the timeline
HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(searchUrl);
var timelineHeaderFormat = "{0} {1}";
timeLineRequest.Headers.Add("Authorization",
string.Format(timelineHeaderFormat, twitAuthResponse.token_type,
twitAuthResponse.access_token));
timeLineRequest.Method = "Get";
WebResponse timeLineResponse = timeLineRequest.GetResponse();
var timeLineJson = string.Empty;
using (timeLineResponse)
{
using (var reader = new StreamReader(timeLineResponse.GetResponseStream()))
{
timeLineJson = reader.ReadToEnd();
}
}
return timeLineJson;
}
You also need to add this to the web or app.config in the appsettings: 您还需要在appsettings中将其添加到web或app.config中:
<add key="searchFormat" value="https://api.twitter.com/1.1/search/tweets.json?q={0}"/>
I will update my the GitHub project to include this later today at: 我将在今天晚些时候更新GitHub项目,以将其包括在内:
https://github.com/andyhutch77/oAuthTwitterTimeline https://github.com/andyhutch77/oAuthTwitter时间轴
There will be some code to un-comment to display the search. 将有一些代码需要取消注释才能显示搜索。 I might have to re-think the project name now it has searching in it too.
我可能现在不得不重新考虑项目名称,因为它也在其中搜索。
Please note if it does not work you may have issues with the limit on your application at the twitter end. 请注意,如果它不起作用,则可能是Twitter端的应用程序限制有问题。 I have also read that some people have problems on shared IP adresses'.
我还读到有些人在共享IP地址方面存在问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.