Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication for twitter API and Rest Call

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

like image 517
benza Avatar asked Oct 30 '12 09:10

benza


1 Answers

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
});
like image 98
maxdec Avatar answered Oct 05 '22 23:10

maxdec