I am trying to obtain a request token from Twitter with this code:
NSMutableURLRequest *mURLRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"]];
mURLRequest.HTTPMethod = @"POST /oauth/request_token HTTP/1.1";
[mURLRequest setValue:@"User-Agent" forHTTPHeaderField:@"Coupled HTTP"];
[mURLRequest setValue:@"OAuth oauth_callback=\"http%3A%2F%2Fbytolution.com\"" forHTTPHeaderField:@"Authorization"];
[mURLRequest setValue:@"api.twitter.com" forHTTPHeaderField:@"Host"];
[mURLRequest setValue:@"Accept" forHTTPHeaderField:@"*/*"];
NSHTTPURLResponse *urlResponse;
NSError *error;
NSError *serializationError;
NSData *responseData = [NSURLConnection sendSynchronousRequest:mURLRequest returningResponse:&urlResponse error:&error];
NSLog(@"data: %@, response:%@, error: %@", [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&serializationError], urlResponse.allHeaderFields, error);
But all I get back is:
data: (null), response:{
Connection = close;
"Content-Length" = 0;
}, error: (null)
Here is Twitter`s documentation for this topic.
Thanks for your help!
Multiple errors (not sure if the list is complete, this is what I found at first glance):
@"POST"
, not@"POST foo"
,@"POST or I will kill you"
or anything else;@"OAuth oauth_callback=\""
should be@"oauth_callback=\""
;oauth_consumer_key
,oauth_signature
,oauth_signature_method
,oauth_nonce
andoauth_timestamp
;After two days of frustration and cursing Twitter, I finally managed to do this. Here is my implementation. The class which will be used to make the request is "BL_TwitterRequest". This is only for obtaining the twitter request token.
BL_TwitterRequest.h:
Add the following NSString category at the top of the implementation class (BL_TwitterRequest.m). This will be used to get the URL encoded version of NSString.
Add the below function in "BL_TwitterRequest.m". It will be used to generate the oAuth nonce. The function basically generates a random string of a specified length.
Include the Base64 library from here. All you have to do is drag the "Base64.h" and "Base64.m" files into your project. Add the following imports:
Add another function as specified below. This will be used to get the HMAC-SHA1 value.
Now for the main function that will make the request.
Now just implement the NSURLConnection delegate methods to get the response.
If everything goes well the 'resultString' will have the oauth_token and oauth_token_secret.
To make the call just do the following:
Remember any missed spaces or commas can result in an error.