栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Twitter API 1.1 oAuth验证并请求用户的时间轴

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Twitter API 1.1 oAuth验证并请求用户的时间轴

这是我通过一个简单的示例所做的工作。

我必须在以下位置从Twitter生成oAuth使用者密钥和机密:

https://dev.twitter.com/apps/new

我首先对身份验证对象进行反序列化以获取令牌,然后再键入以对时间轴调用进行身份验证。

时间轴调用只是读取json,因为这是我需要做的所有事情,您可能想要将其反序列化为一个对象。

我已经在以下位置为此创建了一个项目:https
:
//github.com/andyhutch77/oAuthTwitterWrapper

更新 -我已经更新了github项目,以包括asp .net Web应用程序和mvc应用程序示例演示以及nuget安装。

// You need to set your own keys and screen namevar oAuthConsumerKey = "superSecretKey";var oAuthConsumerSecret = "superSecretSecret";var oAuthUrl = "https://api.twitter.com/oauth2/token";var screenname = "aScreenName";// Do the Authenticatevar 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-urlenpred;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 objectTwitAuthenticateResponse 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 timelinevar 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; }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/398945.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号