Use the HMAC-SHA1 signature algorithm as defined by the [RFC2104] to sign the request where text is the Signature Base String and key is the concatenated values of the Consumer Secret and Access Secret separated by an ‘&’ character (show ‘&’ even if Access Secret is empty as some methods do not require an Access Token).
The calculated digest octet string,first base64-encoded per [RFC2045],then escaped using the [RFC3986] percent-enCoding (%xx) mechanism is the 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 stringNsstring *randomString = [self genRandString]; //see deFinition belowNSLog(@"%@",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 belowNSLog(@"encodedStr: %@",encodedStr);Nsstring *encodedString = [self urlEncodeValue:encodedStr]; //see deFinition belowNSLog(@"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 charsNSMutableString *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下载我的项目来查看问题
解决方法 我自己找到了代码中的错误.近80%的问题已经解决.我在这个地方做错了: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 timestampNSTimeInterval intervalfloat = [[NSDate date] timeIntervalSince1970];int interval = (int) intervalfloat;NSLog(@"time interval: %d",interval);//for oauth_nonce random stringNsstring *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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)