Ios 无效签名:oauth_签名

Ios 无效签名:oauth_签名,ios,oauth,signature,Ios,Oauth,Signature,我正在尝试生成oauth_签名以使用Fatsecret API,但收到无效签名错误-无法找出原因。我尝试按照上面提到的所有步骤(参见步骤2)尽可能准确地生成签名值。他们说: 使用[RFC2104]定义的HMAC-SHA1签名算法对请求进行签名,其中文本是签名基字符串,密钥是消费者机密和访问机密的串联值,由“&”字符分隔(显示“&”,即使访问机密为空,因为某些方法不需要访问令牌) 计算出的摘要八位字节字符串是oauth_签名,该字符串首先根据[RFC2045]进行base64编码,然后使用[RFC

我正在尝试生成oauth_签名以使用Fatsecret API,但收到无效签名错误-无法找出原因。我尝试按照上面提到的所有步骤(参见步骤2)尽可能准确地生成签名值。他们说:

使用[RFC2104]定义的HMAC-SHA1签名算法对请求进行签名,其中文本是签名基字符串,密钥是消费者机密和访问机密的串联值,由“&”字符分隔(显示“&”,即使访问机密为空,因为某些方法不需要访问令牌)

计算出的摘要八位字节字符串是oauth_签名,该字符串首先根据[RFC2045]进行base64编码,然后使用[RFC3986]百分比编码(%xx)机制进行转义

对于base64编码,我使用

步骤一如下:

-(void)viewDidLoad
{
NSTimeInterval intervalFloat=[[NSDate date]timeIntervalSince1970];
int interval=(int)intervalFloat;
NSLog(@“时间间隔:%d”,间隔);
//对于oauth_nonce随机字符串
NSString*randomString=[self-genRandString];//参见下面的定义
NSLog(@“%@”,随机字符串);
NSString*requestString=[NSString stringWithFormat:@“POST&http%3A%2F%2F平台.fatsecret.com%2fsrest%2Fserver.api&format%3Djson%26方法%3Dprofile.创建%26oauth_消费者密钥%3Db753c99ccxxxxxx%26oauth nonce%3D%@%26oauth_签名方法%3DHMAC-SHA1%26oauth_时间戳%3D%d%26oauth_版本%3D1.0”,随机字符串,间隔];
NSString*secret=@“3959096C04xxxxxxx&”;
NSString*encodedStr=[self-hmacsha1:requestString-secret:secret];//参见下面的定义
NSLog(@“编码器:%@”,编码器);
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=b753c99ccxxxxxx&oauth_nonce=%@&oauth_签名=%@&oauth_签名方法=HMAC-SHA1&oauth_时间戳=%d&oauth_版本=1.0”,随机字符串,编码字符串,间隔]];
_请求=[AsiformDataRequestRequestWithURL:url];
[_requestsetpostValue:@“json”forKey:@“format”];
[_requestsetpostValue:@“profile.create”forKey:@“method”];
[_requestsetpostValue:@“b753c99ccxxxxxx”forKey:@“oauth_consumer_key”];
[_requestsetpostValue:randomString forKey:@“oauth_nonce”];
[_请求setPostValue:encodedString forKey:@“oauth_签名”];
[_请求setPostValue:@“HMAC-SHA1”forKey:@“oauth_签名方法”];
[\u请求setPostValue:[NSNumber numberwhithint:interval]forKey:@“oauth\u时间戳”];
[_请求setPostValue:@“1.0”forKey:@“oauth_版本”];
[_requestsetdelegate:self];
_request.timeOutSeconds=60.0;
[_请求启动同步];
}
我在上述代码中使用的方法定义如下:

-(NSString*)hmacsha1:(NSString*)数据机密:(NSString*)密钥{
const char*cKey=[key cStringUsingEncoding:NSASCIIStringEncoding];
常量字符*cData=[数据cStringUsingEncoding:NSASCIIStringEncoding];
无符号字符cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1、cKey、strlen(cKey)、cData、strlen(cData)、cHMAC);
NSData*HMAC=[[NSData alloc]initWithBytes:cHMAC长度:sizeof(cHMAC)];
NSString*hash=[QSStrings encodeBase64WithData:HMAC];
NSLog(@“哈希:%@”,哈希);
返回散列;
}
NSString*字母=@“abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzo123456789”;
-(NSString*)genRandString{
//固定长度为4个字符
NSMutableString*randomString=[NSMutableString stringWithCapacity:4];

对于(int i=0;i='a'&&thisChar='a'&&thisChar='0'&&thisChar至少我自己发现了代码中的错误。近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",randomString,interval];
在这里,我对基本字符串进行了预编码。当我使用某种格式初始化字符串时,它将
http%3A%2F%2F
作为某种未知格式,将其替换为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",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];

}

我希望这对某人有所帮助。

这是您尝试的两条腿OAuth身份验证吗?我也同样感到困惑,无论我如何尝试正确生成签名,最终都会得到一个“无效签名”身体。我也尝试过你的方法,但我没能得到。你在上述方法中有多成功?是的。我在浏览器上测试了从上述代码生成的字符串,它工作正常。但我不知道为什么在模拟器上运行会出现签名错误。你在设备上尝试过吗?我没有在浏览器中检查生成的字符串!我正在测试我t仅在模拟器中,因为我的开发者帐户尚未更新。好吧,如果它在浏览器和设备中工作,我想应该可以。我将在浏览器中检查生成的URL,并在下班后通知您。谢谢。感谢您宝贵的输入,我能够解决问题。我已经记录了我所做的一切,并且他在这里准确描述了我的问题: