C# 验证并请求用户';TwitterAPI 1.1OAuth的时间线
今天早上,我收到了令人恐惧的“Twitter REST API v1不再活动”。请迁移到API v1.1。我的一些网站出现错误 之前,我一直在使用javascript/json调用?显示时间线 由于这不再可用,我需要采用新的1.1 API流程 我需要使用HttpWebRequest对象而不是第三方应用程序执行以下操作:C# 验证并请求用户';TwitterAPI 1.1OAuth的时间线,c#,asp.net,twitter,webclient,twitter-oauth,C#,Asp.net,Twitter,Webclient,Twitter Oauth,今天早上,我收到了令人恐惧的“Twitter REST API v1不再活动”。请迁移到API v1.1。我的一些网站出现错误 之前,我一直在使用javascript/json调用?显示时间线 由于这不再可用,我需要采用新的1.1 API流程 我需要使用HttpWebRequest对象而不是第三方应用程序执行以下操作: 使用oauth密钥和密码进行身份验证 进行身份验证调用以回拉以显示用户时间线 下面是我在一个简单的例子中所做的工作 我必须在Twitter上生成oAuth用户密钥和密码: 我首
下面是我在一个简单的例子中所做的工作 我必须在Twitter上生成oAuth用户密钥和密码: 我首先反序列化了身份验证对象,以获取令牌并返回类型,以便对时间线调用进行身份验证 timeline调用只是读取json,因为这就是我所需要做的,您可能希望自己将其反序列化为一个对象 我已为此在以下位置创建了一个项目: 更新-我已经更新了github项目,将asp.net web应用程序和mvc应用程序示例演示和nuget安装都包括在内
// 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; }
}
//您需要设置自己的键和屏幕名称
var oAuthConsumerKey=“superSecretKey”;
var OAuthConsumerCret=“superSecretSecret”;
var OAuthull=”https://api.twitter.com/oauth2/token";
var screenname=“aScreenName”;
//进行身份验证吗
var authHeaderPerformat=“基本{0}”;
var authHeader=string.Format(authHeaderPerformat,
Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey))+“:”+
Uri.EscapeDataString((OAuthConsumerCret)))
));
var postBody=“授予\类型=客户\凭证”;
HttpWebRequest authRequest=(HttpWebRequest)WebRequest.Create(oauthull);
添加(“授权”,authHeader);
authRequest.Method=“POST”;
authRequest.ContentType=“application/x-www-form-urlencoded;charset=UTF-8”;
authRequest.AutomaticDecompression=DecompressionMethods.GZip | DecompressionMethods.Deflate;
使用(Stream-Stream=authRequest.GetRequestStream())
{
byte[]content=ascienceoding.ASCII.GetBytes(postBody);
stream.Write(content,0,content.Length);
}
Add(“接受编码”、“gzip”);
WebResponse authResponse=authRequest.GetResponse();
//反序列化为对象
TwitAuthenticateResponse twitAuthResponse;
使用(authResponse)
{
使用(var reader=newstreamreader(authResponse.GetResponseStream()){
JavaScriptSerializer js=新的JavaScriptSerializer();
var objectText=reader.ReadToEnd();
twitAuthResponse=JsonConvert.DeserializeObject(objectText);
}
}
//完成时间表
var timelineFormat=”https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_reply=1&count=5”;
var timelineUrl=string.Format(timelineFormat,屏幕名称);
HttpWebRequest timeLineRequest=(HttpWebRequest)WebRequest.Create(timelineUrl);
var timelineHeaderFormat=“{0}{1}”;
timeLineRequest.Headers.Add(“授权”,string.Format(timelineHeaderFormat,twituthresponse.token_type,twituthresponse.access_token));
timeLineRequest.Method=“Get”;
WebResponse timeLineResponse=timeLineRequest.GetResponse();
var timeLineJson=string.Empty;
使用(timeLineResponse)
{
使用(var reader=newstreamreader(timeLineResponse.GetResponseStream())
{
timeLineJson=reader.ReadToEnd();
}
}
公共类TwitAuthenticateResponse{
公共字符串标记\u类型{get;set;}
公共字符串访问\u令牌{get;set;}
}
创建了一个只支持JS的解决方案,可以在不使用新API的情况下在您的站点上获取Twitter帖子-现在也可以指定推文数量:混合使用,我有传统和mvc。您看到了吗?twitter开发网站上还有一个第三方库的列表——有一些是C#库。@Jimbo嗨,是的,谢谢。我需要避免使用第三方库。原因是我需要在许多不同类型的应用程序(mvc和传统应用程序)、CMS、门户等中使用它。我几乎已经完成了,如果它有效,我会发布答案。@hutchonoid是否可以在网络表单中对所有用户进行关键字搜索?@PhillHealey是的,我相信你可以。基本上,twitter api中可用的任何内容都可以公开,尽管您可能必须在github上扩展/修改源代码。您应该将其放入一个类中,并将其放在github上供其他人使用;)非常感谢大家分享这篇文章-它真的帮助我恢复了状态并快速运行!解决方案中的一个建议是允许通过构造函数或公开属性将参数传递到OAuthTwitterWrapper对象中。在我的例子中,我希望能够根据页面上可用的不动产来改变要显示的推文数量。尽管如此,这是一个很好的解决方案,谢谢!没问题,很高兴看到它被使用。我会将这些更改添加到您建议的项目中。非常感谢。哇,太快了!谢谢:-)很棒的帖子,这应该在Twitter文档页面上,更容易理解!