Authentication for twitter API and Rest Call

只愿长相守 提交于 2019-12-21 13:29:06

问题


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...

Example: I want to do this request: https://api.twitter.com/1.1/search/tweets.json?q=q=%23freebandnames

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????

THanks in advance for answers


回答1:


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

or

Do it yourself: everything is explained in the documentation.

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

  • An application encodes its consumer key and secret into a specially encoded set of credentials.
  • An application makes a request to the POST oauth2/token endpoint to exchange these credentials for a bearer token.
  • When accessing the REST API, the application uses the bearer token to authenticate.

The steps are detailed in the documentation.

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
});


来源:https://stackoverflow.com/questions/13136092/authentication-for-twitter-api-and-rest-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!