0
我正在開發一個應用程序,需要使用OAuth1.0調用API。OAMutableURLRequest:簽名invalide
我可以成功獲取訪問令牌。但是當我嘗試發佈Json數據的時候。它給我錯誤。 帶請求的後JSON數據代碼。
-(void)sampleRequest2{
// create URL from string
NSURL* url = [[NSURL alloc] initWithString:@"http://myurl"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Json" ofType:@"rtf"];
NSData *data = [NSData dataWithContentsOfFile:path];
// create url request
OAMutableURLRequest *urlRequest = [[[OAMutableURLRequest alloc] initWithURL:url
consumer:consumer
token:accessToken
realm:nil
signatureProvider:nil] autorelease];
OARequestParameter* callbackParam = [[OARequestParameter alloc] initWithName:@"oauth_callback" value:callback];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setParameters:[NSArray arrayWithObjects:callbackParam,nil]];
[urlRequest prepare];
__weak NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[data length]];
// Create entire body
NSMutableData* dataRequestBody = [NSMutableData data];
NSString* boundary = @"----IMAGE_UPLOAD";
[dataRequestBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[dataRequestBody appendData:[[NSString stringWithFormat:@"Content-Length: %@\r\n\r\n",postLength] dataUsingEncoding:NSUTF8StringEncoding]];
[dataRequestBody appendData:[@"Content-Type: application/json\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[dataRequestBody appendData:data];
[dataRequestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:dataRequestBody];
OADataFetcher *dataFetcher = [[OADataFetcher alloc]init];
[dataFetcher fetchDataWithRequest:urlRequest delegate:self completionBlock:^(OAServiceTicket *ticket, NSMutableData *data) {
if (data == nil) {
}else{
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON Response: %@",json);
}
}
failedBlock:^{
NSLog(@"Failed");
}];
}
錯誤響應:
JSON Response: {
messages = {
error = (
{
code = 401;
message = "oauth_problem=signature_invalid";
}
);
};
}