[英]Twitter API Integration - The remote server returned an error: (401) Unauthorized
[英]Twitter Search API The remote server returned an error: (401) Unauthorized
我有一個應用程序試圖對Twitter feed進行簡單的GET調用,但是由於上面的錯誤而失敗。 在調用下面的代碼之前,我能夠成功獲取一個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();
好的,這正是您需要做的,我已經測試過了,這是一個可行的解決方案:
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;
}
您還需要在appsettings中將其添加到web或app.config中:
<add key="searchFormat" value="https://api.twitter.com/1.1/search/tweets.json?q={0}"/>
我將在今天晚些時候更新GitHub項目,以將其包括在內:
https://github.com/andyhutch77/oAuthTwitter時間軸
將有一些代碼需要取消注釋才能顯示搜索。 我可能現在不得不重新考慮項目名稱,因為它也在其中搜索。
請注意,如果它不起作用,則可能是Twitter端的應用程序限制有問題。 我還讀到有些人在共享IP地址方面存在問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.