且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

twitter API 和 Rest Call 的认证

更新时间:2021-10-24 05:49:17

你可以使用这个 javascript 库:codebird-js 带有仅应用程序身份验证".

You can use this javascript library: codebird-js with the "Application-only auth".

自己动手:文档中解释了所有内容>.

基本上你需要遵循 3 个步骤(你应该先做 2 个步骤一次):

Basically you need to follow 3 steps (and you should do the 2 first just once):

  • 应用程序将其消费者密钥和秘密编码为一组经过特殊编码的凭据.
  • 应用程序向 POST oauth2/token 端点发出请求,以将这些凭据交换为不记名令牌.
  • 在访问 REST API 时,应用程序使用不记名令牌进行身份验证.

文档中详细介绍了这些步骤.

The steps are detailed in the documentation.

您可以单独执行前 2 步,当您获得不记名令牌时,您需要在每个请求中添加特定的 HTTP 标头(授权)以及您的不记名令牌(这是第 3 步).使用 jQuery,它可能是这样的:

You can do the first 2 separately and when you get your bearer token, you need to add a specific HTTP Header (Authorization) in each request with your bearer token (that's the 3rd step). With jQuery it could be something like that:

$.ajax({
  headers: { Authorization: 'Bearer '+$my_bearer_token },
  url: 'https://api.twitter.com/1.1/search/tweets.json?q='+$search
}).done(function (data) {
  // Play with the data
});