Atlast I myself calculated the errors in the code. The problem is solved by 80%. I was wrong in this place:
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];
Here I precoded the base line. Since I initialize a string with some format, it took http%3A%2F%2F as some unknown format, replacing it with 0. Therefore, I replaced the whole code as follows:
- (void)viewDidLoad { [super viewDidLoad]; //for timestamp NSTimeInterval intervalFloat = [[NSDate date] timeIntervalSince1970]; int interval = (int) intervalFloat; NSLog(@"time interval: %d",interval); //for oauth_nonce random string NSString *randomString = [self genRandString]; NSLog(@"%@",randomString); NSString *actualString = [NSString stringWithFormat:@"format=json&method=profile.create&oauth_consumer_key=b753c99ccd8****&oauth_nonce=%@&oauth_signature_method=HMAC-SHA1&oauth_timestamp=%d&oauth_version=1.0",randomString,interval]; NSString *firstEncode = [self urlEncodeValue:actualString]; NSLog(@"first encode: %@",firstEncode); NSMutableString *requestString = [[NSMutableString alloc] initWithString:@"GET&http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&"]; [requestString appendString:firstEncode]; NSLog(@"base str: %@",requestString); NSString *secret = @"395********&"; NSString *encodedStr = [self hmacsha1:requestString secret:secret]; NSLog(@"encodedStr: %@",encodedStr); NSString *encodedString = [self urlEncodeValue:encodedStr]; NSLog(@"encodedString: %@",encodedString); NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://platform.fatsecret.com/rest/server.api?format=json&method=profile.create&oauth_consumer_key=b753c99cc*******&oauth_nonce=%@&oauth_signature=%@&oauth_signature_method=HMAC-SHA1&oauth_timestamp=%d&oauth_version=1.0",randomString, encodedString,interval]]; NSLog(@"url: %@",url); _request = [ASIFormDataRequest requestWithURL:url]; [_request setPostValue:@"json" forKey:@"format"]; [_request setPostValue:@"profile.create" forKey:@"method"]; [_request setPostValue:@"b753c9*********" 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 setRequestMethod:@"GET"]; [_request addRequestHeader:@"Content-Type" value:@"application/json"]; [_request setDelegate:self]; _request.timeOutSeconds = 60.0; [_request startAsynchronous]; }
I hope this helps someone.
Nightfury
source share