使用 Twitter API 1.1 oAuth 验证和请求用户的时间线 [英] Authenticate and request a user's timeline with Twitter API 1.1 oAuth

查看:33
本文介绍了使用 Twitter API 1.1 oAuth 验证和请求用户的时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上我收到了可怕的Twitter REST API v1 不再处于活动状态"的消息.请迁移到 API v1.1.我的一些网站出错了.

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.

以前我一直使用 javascript/json 对 http://api.twitter.com/进行这些调用1/statuses/user_timeline.json?显示时间线.

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.

由于不再可用,我需要采用新的 1.1 API 流程.

As this is no longer available I need to adopt the new 1.1 API process.

我需要使用 HttpWebRequest 对象而不是第 3 方应用程序执行以下操作:

I need to do the following using HttpWebRequest objects not a 3rd party application:

  1. 使用 oauth 密钥和秘密进行身份验证
  2. 发出经过身份验证的调用以返回显示用户时间线

推荐答案

这是我在一个简单示例中为使其工作所做的工作.

Here is what I did to get this working in a simple example.

我必须从 Twitter 生成一个 oAuth 消费者密钥和秘密:

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.

时间线调用只是读取 json,因为这就是我需要做的,您可能希望自己将其反序列化为一个对象.

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.

我为此创建了一个项目:https://github.com/andyhutch77/oAuthTwitterWrapper

I have created a project for this at : https://github.com/andyhutch77/oAuthTwitterWrapper

更新 - 我已经更新了 github 项目以包含 asp .net web 应用程序和mvc 应用示例演示和 nuget 安装.

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 1.1 oAuth 验证和请求用户的时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆