【问题标题】:Authenticate and request a user's timeline with Twitter API 1.1 oAuth使用 Twitter API 1.1 oAuth 验证和请求用户的时间线
【发布时间】:2013-06-08 17:06:27
【问题描述】:

今天早上我收到了可怕的“Twitter REST API v1 不再活动”。请迁移到 API v1.1。我的一些网站出现错误。

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

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

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

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

【问题讨论】:

  • 一种混合物,我有传统和mvc。
  • 你见过 this 吗? twitter 开发网站上还有一个第三方库列表 - 有一些 C# 库。
  • @Jimbo 嗨,是的,谢谢。我需要避免使用 3rd 方库。原因是我需要在许多不同类型的应用程序(mvc 和传统应用程序)、CMS、门户网站等中使用它。我几乎完成了,如果它有效,我会发布答案。
  • @hutchonoid 是否可以对 WebForm 中的所有用户进行关键字搜索???
  • @PhillHealey 是的,我相信你可以。基本上任何可以从 twitter api 获得的东西都可以公开,尽管您可能必须在 github 上扩展/修改源代码。

标签: c# asp.net twitter webclient twitter-oauth


【解决方案1】:

这是我在一个简单的示例中所做的工作。

我必须在以下位置从 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 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; }
}

【讨论】:

  • 你应该把它塞进一个类,放到github上供其他人使用;)
  • 感谢一百万的分享 - 它真的帮助我快速恢复并运行!您的解决方案的一个建议是允许通过构造函数或通过公开属性将参数传递到 OAuthTwitterWrapper 对象中。就我而言,我希望能够根据页面上可用的房地产来改变要显示的推文数量。都一样,一个很好的解决方案,谢谢!
  • 没问题,很高兴看到它被使用。我会将这些更改添加到您建议的项目中。非常感谢。
  • 哇,真快!谢谢:-)
  • 很棒的帖子,这应该在 Twitter 文档页面上,更容易理解!
【解决方案2】:

创建了一个仅 JS 的解决方案,无需使用新 API 即可在您的网站上获取 Twitter 帖子 - 现在也可以指定推文数量:http://goo.gl/JinwJ

【讨论】:

    猜你喜欢
    • 2014-05-04
    • 2013-03-30
    • 1970-01-01
    • 2017-12-30
    • 2011-06-23
    • 2021-08-31
    • 2012-10-29
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多