jquery -/statuses/user_timeline 仍然可以通过 JavaScript 读取吗?

标签 jquery twitter twitter-oauth

我之前使用过一些 jQuery 来阅读 twitter 上的推文:

$.ajax('https://api.twitter.com/1/statuses/user_timeline.json', {
    crossDomain: true,
    data: {
        screen_name: 'twitterapi',
        count: 5
    },
    dataType: 'jsonp'
}).done(function (tweets) {
    console.log(tweets);
});

由于 Twitter 正在弃用其 1.0 API,并要求 1.1 API 使用 OAuth,我一直在尝试弄清楚是否仍然可以以相同的方式获取推文数据。

只需将网址更改为:

https://api.twitter.com/1.1/statuses/user_timeline.json

导致没有消息的 400 Bad Request 响应。

我知道有一个 Twitter 工具可以为请求创建 OAuth 签名,但我不确定如何将其与 JSONP 请求一起使用,甚至如果它可以与 JSONP 一起使用请求。

Twitter 1.1 API 中是否仍然可以读取用户的时间线?

最佳答案

如果你看一下 Twitter 的 Error Codes & Responses ,状态代码400表示:

The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting. In API v1.1, a request without authentication is considered invalid and you will get this response.

因此,虽然 400 代码过去表示您超出了速率限制,但现在当请求未经身份验证时也会返回该代码。

要验证请求,您必须添加 Oauth Authorization header 。有一些libraries这可以帮助解决这个问题,但问题是要生成 Oauth 签名,您必须将应用程序的 key (包括 key )硬编码到客户端代码中,这会将其暴露给最终用户(而不是好主意)。

最好的办法是在服务器上设置代理 - 让服务器使用 Oauth header 进行 GET,然后使用 ajax 从服务器获取推文。

关于jquery -/statuses/user_timeline 仍然可以通过 JavaScript 读取吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12480274/

相关文章:

python - 在 Python 中挖掘两个日期之间的推文

android - Twitter + OAuth 集成

android - 我是否每次都必须遵循 OAuth 工作流程(请求 token -> 访问 token -> API 调用)?

javascript - 如何使用 Google 的动态提要控制增加 RSS 提要的数量

javascript - jQuery 脚本只有在函数之上时才有效

python - Twitter API - 如何获取用户的实时推文

ios - 为什么 iOS 应用最近更改了身份验证方法?

javascript - 在绑定(bind)事件之间传递参数

javascript - 如何在Jquery中选择随机文本框?

css - Twitter 徽章毁了我的页脚