Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Application-Only Auth with "Bearer Token"

Short questions:

I've generated an Access Token and an Access Token Secret at apps.twitter.com for my application. How do I use them to send a request to https://api.twitter.com/1.1/statuses/user_timeline.json?

What is a Bearer Token comprised of? Is it the Access Token or the Access Token Secret? Or an encoding of the two?



Bit of context:

I'm trying to make an application that downloads tweets from my twitter timeline without the app user having to authenticate with their twitter account. I understand that I must use Application-Only authentication, and that the documentation (https://dev.twitter.com/docs/auth/application-only-auth) states that I need to use a Consumer Key and Consumer Secret to request a Bearer Token. But if I've already generated the tokens at apps.twitter.com using the generate button:


Screen shot of my app on the apps.twitter.com page


surely I can just hardcode these into my app and pass them along as the Bearer Token in some way? Like this I'd expect:

#define kTwitterBearerToken @"123456"    

NSURL *twitterFeedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=kylejm_&count=10"];
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:twitterFeedURL];
[URLRequest setHTTPMethod:@"POST"];
[URLRequest setValue:[NSString stringWithFormat:@"Bearer %@", kTwitterBearerToken] forHTTPHeaderField:@"Authorization"];
NSURLResponse *URLResponse;
NSError *URLerror;
NSData *tweetData = [NSURLConnection sendSynchronousRequest:URLRequest returningResponse:&URLResponse error:&URLerror];
NSError *JSONError;
NSArray *tweets = [NSJSONSerialization JSONObjectWithData:tweetData options:0 error:&JSONError];
NSLog(@"%@", tweets);

Thanks in advance for answers and help!

Kyle

P.S. I've looked at STTwitter, but think that it's a bit unnecessary to use it when what I'm trying to achieve is so simple (at least it seems simple to just pass the pre-generated token to me anyway)...

like image 436
kylejm Avatar asked Apr 19 '14 20:04

kylejm


People also ask

What authentication does Twitter use?

Most of Twitter's Enterprise APIs require HTTP Basic Authentication. This consists of a valid email address and password combination passed as an authorization header for each API request.

Does Twitter use OAuth?

Twitter allows you to obtain user access tokens through the 3-legged OAuth flow, which allows your application to obtain an access token and access token secret by redirecting a user to Twitter and having them authorize your application.

What is the Twitter API App bearer token?

A bearer token allows developers to have a more secure point of entry for using the Twitter APIs, and are one of the core features of OAuth 2.0. Authentication, which uses a Bearer Token, is also known as application-only authentication.

How do I authenticate with bearer token?

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.


1 Answers

To get access token for twitter application only authentication:

For more details: Twitter-App-Only-Authentication-iOS

  1. Create an application on your "https://apps.twitter.com" acount
  2. Get "kConsumerKey" and "kConsumerSecretKey"
  3. Get Base64EncodedBearerToken using "kConsumerKey" and "kConsumerSecretKey"
  4. Make "kTwitterAuthAPI" call with "Basic Authorization"
  5. Get "access_token" and use it in "Bearer Authorization" calls

Check out the code for more clarifications!

like image 141
Babu Lal Avatar answered Oct 27 '22 03:10

Babu Lal