Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS HTTP Request Example

I am trying make a call to the fitbit API. I am unsure how to input the HTTP request shown below into my Objective C code in order to make this call and handle the response.

POST /oauth/request_token HTTP/1.1
Host: api.fitbit.com
Authorization: OAuth oauth_consumer_key="fitbit-example-client-application",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1270248082",
oauth_nonce="161822064",
oauth_callback="http%3A%2F%2Fexample.fitbit.com%2Fapp%2FcompleteAuthorization",
oauth_signature="Omf%2Bls2gn%2BDlghq245LRIyfMdd8%3D"
oauth_version="1.0"

A simple example would be helpful. Thank you.

like image 803
Khrisendat Avatar asked Oct 22 '22 21:10

Khrisendat


2 Answers

I suggest using an OAuth library to handle the OAuth signature generation. It can be a pain in the ass to wrangle the Authorization header. I've used oauthconsumer with success.

Code sample:

OAConsumer *consumer = [[OAConsumer alloc] initWithKey:oauthConsumerKey secret:oauthConsumerSecret];
OAToken *token = [[OAToken alloc] initWithKey:oauthAccessToken secret:oauthAccessTokenSecret];
OAHMAC_SHA1SignatureProvider *provider = [[OAHMAC_SHA1SignatureProvider alloc] init];

OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] consumer:consumer token:token realm:nil signatureProvider:provider];
[request prepare];

NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

In this example, the 5 strings you will provide are:

oauthConsumerKey
oauthConsumerSecret
oauthAccessToken
oauthAccessTokenSecret
urlString
like image 131
Steven Avatar answered Oct 25 '22 19:10

Steven


I am trying to do the same thing and oauthconsumer looks quite nice.

Is it because I am not getting the oauthAccessTokenSecret? [edit] Yes, it was.

I keep getting: "This page is no longer valid. It looks like you provided an invalid token or someone already used the token you provided. Please return to the site or application which sent you to this page and try again."

[edit] This is because it didn't have the correct token on the url string.

like image 20
mevdev Avatar answered Oct 25 '22 17:10

mevdev