twitter API 和 Rest Call 的认证 [英] Authentication for twitter API and Rest Call
问题描述
我一直在使用 FB api 进行一些简单的演示,通过身份验证,一切都非常简单.现在我必须用 twitter v1.1 做一些类似的事情,但有一些我真的不明白......
I have been using FB api for some simple demo and everything was quite easy with the authentication. Now I have to do something similar with twitter v1.1 but there is something that I really don't understand...
示例:我想做这个请求:https://api.twitter.com/1.1/search/tweets.json?q=q=%23freebandnames
Example: I want to do this request: https://api.twitter.com/1.1/search/tweets.json?q=q=%23freebandnames
问题是我必须通过身份验证,有人有一些例子吗?我不想创建 Twitter 连接,因为我不需要将不同的用户连接到我的应用程序.我只需要执行一些简单的搜索请求,但我无法理解如何使用身份验证参数.我必须使用哪种类型的 Ajax 请求才能执行经过身份验证的 REST 请求???(显然我有我的秘密令牌和我的访问秘密令牌)但如何使用它们????
the problem is that I have to be authenticated, anyone have some examples? I don't want to create a twitter connection because I don't need different users to be connected to my applicaiton. I have just to perform some simple search request but I can't understand how to use the authentication parameters. Which type of Ajax request I have to use in order to perform the REST request authenticated??? (Obviously I have my secret token and my access secret token) but how to use them????
提前感谢回答
推荐答案
你可以使用这个 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
});
这篇关于twitter API 和 Rest Call 的认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!