Invalid signature: oauth_signature in iOS

217 views
Skip to first unread message

Anum Amin

unread,
Nov 4, 2012, 6:34:30 AM11/4/12
to fatsecret-p...@googlegroups.com
I am trying to produce oauth_signature to use **Fatsecret API**, but getting invalid signature error - can't figure out why. I tried to follow all steps mentioned for Signature value generation as accurately as possible.

For base64 encoding, I used QSStrings.h

 Steps I coded are as follows:

    - (void)viewDidLoad
    {
    NSTimeInterval intervalFloat = [[NSDate date] timeIntervalSince1970];
    int interval = (int) intervalFloat;
    NSLog(@"time interval: %d",interval);
    
    //for oauth_nonce random string
    NSString *randomString = [self genRandString]; //see definition below
    NSLog(@"%@",randomString);
    
    NSString *requestString = [NSString stringWithFormat:@"POST&http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&format%3Djson%26method%3Dprofile.create%26oauth_consumer_key%3Db753c99ccxxxxxx%26oauth_nonce%3D%@%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D%d%26oauth_version%3D1.0",randomString,interval];
    NSString *secret = @"3959096c04xxxxxxxx&";
    
    NSString *encodedStr = [self hmacsha1:requestString secret:secret]; //see definition below
    NSLog(@"encodedStr: %@",encodedStr);
    
    NSString *encodedString = [self urlEncodeValue:encodedStr]; //see definition below
    NSLog(@"encodedString: %@",encodedString);
    
               
    _request = [ASIFormDataRequest requestWithURL:url];
    [_request setPostValue:@"json" forKey:@"format"];
    [_request setPostValue:@"profile.create" forKey:@"method"];
    [_request setPostValue:@"b753c99ccxxxxxx" forKey:@"oauth_consumer_key"];
    [_request setPostValue:randomString forKey:@"oauth_nonce"];
    [_request setPostValue:encodedString forKey:@"oauth_signature"];
    [_request setPostValue:@"HMAC-SHA1" forKey:@"oauth_signature_method"];
    [_request setPostValue:[NSNumber numberWithInt:interval] forKey:@"oauth_timestamp"];
    [_request setPostValue:@"1.0" forKey:@"oauth_version"];

    [_request setDelegate:self];
    _request.timeOutSeconds = 60.0; 
    [_request startAsynchronous];
    
    }

Definitions for methods I used in code above are as follows:

    - (NSString *)hmacsha1:(NSString *)data secret:(NSString *)key {
    
    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
    
    unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    
    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    
    NSString *hash = [QSStrings encodeBase64WithData:HMAC];
    
    NSLog(@"Hash: %@", hash);  
    
    return hash;
    }

    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    -(NSString *) genRandString {
    //fixing length of 4 chars
    NSMutableString *randomString = [NSMutableString stringWithCapacity: 4];
    
    for (int i=0; i<4; i++) {
        [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
    }
    
    return randomString;
    }

    - (NSString *)urlEncodeValue:(NSString *)str
    {
    NSMutableString * output = [NSMutableString string];
    const unsigned char * source = (const unsigned char *)[str UTF8String];
    int sourceLen = strlen((const char *)source);
    for (int i = 0; i < sourceLen; ++i) {
        const unsigned char thisChar = source[i];
        if (thisChar == ' '){
            [output appendString:@"+"];
        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
                   (thisChar >= 'a' && thisChar <= 'z') ||
                   (thisChar >= 'A' && thisChar <= 'Z') ||
                   (thisChar >= '0' && thisChar <= '9')) {
            [output appendFormat:@"%c", thisChar];
        } else {
            [output appendFormat:@"%%%02X", thisChar];
        }
    }
    return output;   
   
    }

Please anybody help me out. Thanks :(

Parker Wightman

unread,
Nov 29, 2012, 12:17:43 AM11/29/12
to fatsecret-p...@googlegroups.com
I built a library that does all this for you that might be of use to you: https://github.com/mysterioustrousers/FatSecretKit

This is too much code to wrap one's head around in a forum like this, sorry I can't be of more help :-(
Reply all
Reply to author
Forward
0 new messages