问题描述
今天早上我收到了可怕的Twitter的REST API V1不再有效。请迁移到API V1.1。错误我的几个网站。
previously我一直在使用的JavaScript / JSON使这些调用http://api.twitter.com/1/statuses/user_timeline.json?显示时间表。
由于这不再我需要采用新的1.1 API的过程。
我需要做以下使用HttpWebRequest的对象不是一个第三方应用程序:
- 验证使用OAuth密钥和密码
- 请通过身份验证的电话给拉了回来,以显示用户时间表
下面是我做的一个简单的例子来得到这个工作。
我不得不产生从Twitter在一个OAuth使用者密钥和机密:
我第一次反序列化的认证对象,以获得令牌,以验证时间表呼叫类型回来。
时间轴调用只是读取JSON,因为这是所有我需要做的,你可能想自己反序列化到一个对象。
我在创建了项目如下:https://github.com/andyhutch77/oAuthTwitterWrapper
更新 - 我已经更新了GitHub的项目,包括ASP.NET的Web应用程序和放大器; MVC应用程序的例子演示和安装的NuGet
//你需要设置自己的按键和屏幕名称
VAR oAuthConsumerKey =superSecretKey;
VAR oAuthConsumerSecret =superSecretSecret;
VAR oAuthUrl =https://api.twitter.com/oauth2/token;
VAR屏幕名=aScreenName;//执行身份验证
VAR authHeaderFormat =基本{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(授权,authHeader);
authRequest.Method =POST;
authRequest.ContentType =应用/的X WWW的形式urlen codeD;字符集= UTF-8;
authRequest.AutomaticDecom pression = DECOM pressionMethods.GZip | DECOM pressionMethods.Deflate;使用(流流= authRequest.GetRequestStream())
{
字节[] =内容ASCIIEncoding.ASCII.GetBytes(postBody);
stream.Write(含量,0,content.Length);
}authRequest.Headers.Add(接受编码,gzip的);WebResponse类authResponse = authRequest.GetResponse();
//反序列化到一个对象
TwitAuthenticateResponse twitAuthResponse;
使用(authResponse)
{
使用(VAR读者=新的StreamReader(authResponse.GetResponseStream())){
JS的JavaScriptSerializer =新的JavaScriptSerializer();
变种objectText = reader.ReadToEnd();
twitAuthResponse = JsonConvert.DeserializeObject< TwitAuthenticateResponse>(objectText);
}
}//执行时间表
VAR timelineFormat = \"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=5\";
VAR timelineUrl =的String.Format(timelineFormat,屏幕名);
HttpWebRequest的timeLineRequest =(HttpWebRequest的)WebRequest.Create(timelineUrl);
VAR timelineHeaderFormat ={0} {1};
timeLineRequest.Headers.Add(授权,的String.Format(timelineHeaderFormat,twitAuthResponse.token_type,twitAuthResponse.access_token));
timeLineRequest.Method =获取;
WebResponse类timeLineResponse = timeLineRequest.GetResponse();
VAR timeLineJson =的String.Empty;
使用(timeLineResponse)
{
使用(VAR读者=新的StreamReader(timeLineResponse.GetResponseStream()))
{
timeLineJson = reader.ReadToEnd();
}
}
公共类TwitAuthenticateResponse {
公共字符串token_type {搞定;组; }
公共字符串的access_token {搞定;组; }
}
This morning I have received the dreaded 'The Twitter REST API v1 is no longer active. Please migrate to API v1.1.' error in a few of my web sites.
Previously I have been using javascript/json to make these calls to http://api.twitter.com/1/statuses/user_timeline.json? to display a timeline.
As this is no longer available I need to adopt the new 1.1 API process.
I need to do the following using HttpWebRequest objects not a 3rd party application:
- Authenticate using oauth key and secret
- Make an authenticated call to pull back to display users timeline
Here is what I did to get this working in a simple example.
I had to generate an oAuth consumer key and secret from Twitter at:
https://dev.twitter.com/apps/new
I deserialized the authentication object first to get the token and type back in order to authenticate the timeline call.
The timeline call simply reads the json as that is all I need to do, you may want to deserialize it yourself into an object.
I have created a project for this at : https://github.com/andyhutch77/oAuthTwitterWrapper
Update - I have updated the github project to include both asp .net web app & mvc app example demos and nuget install.
// You need to set your own keys and screen name
var oAuthConsumerKey = "superSecretKey";
var oAuthConsumerSecret = "superSecretSecret";
var oAuthUrl = "https://api.twitter.com/oauth2/token";
var screenname = "aScreenName";
// 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
var timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=5";
var timelineUrl = string.Format(timelineFormat, screenname);
HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl);
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();
}
}
public class TwitAuthenticateResponse {
public string token_type { get; set; }
public string access_token { get; set; }
}
这篇关于身份验证,并请求用户的时间表与Twitter API的OAuth 1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!