Search code examples
ios6http-headersfitbit

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.


Solution

  • 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