大佬教程收集整理的这篇文章主要介绍了ios – 签名无效:oauth_signature,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
对于base64编码,我使用了QSStrings.h
我编码的步骤如下:
- (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); NSURL *url = [NSURL URLWithString:[NSString StringWithFormat:@"http://platform.fatsecret.com/rest/server.api?format=json&method=profile.create&oauth_consumer_key=b753c99ccxxxxxx&oauth_nonce=%@&oauth_signature=%@&oauth_signature_method=HMAC-SHA1&oauth_timestamp=%d&oauth_version=1.0",encodedString,interval]]; _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]; }
- (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; }
您可以通过从here下载我的项目来查看问题
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",interval];
这里我预先编码了基本字符串.当我使用某种格式初始化字符串时,它将http://视为某种未知格式,将其替换为0.所以我将整个代码替换为:
- (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",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",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]; }
我希望这可以帮助别人.
以上是大佬教程为你收集整理的ios – 签名无效:oauth_signature全部内容,希望文章能够帮你解决ios – 签名无效:oauth_signature所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。