我正在开发一个需要使用OAuth1.0调用API的应用。
我可以成功获取访问令牌。但是当我尝试在那时发布Json数据时。它给我错误。 带请求的post 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";
}
);
};
}
答案 0 :(得分:0)
我在搜索了两天后终于找到了解决方案。
我需要在创建请求时传递签名提供程序。
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:accessToken realm:realm signatureProvider:[[OAPlaintextSignatureProvider alloc] init]];
用于传递JSON数据。
[request setHTTPBody:[strJson dataUsingEncoding:NSUTF8StringEncoding]];
所以,结局代码是..
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:accessToken realm:realm signatureProvider:[[OAPlaintextSignatureProvider alloc] init]];
[request setHTTPMethod:@"POST"];
[request setParameters:[NSArray arrayWithObjects:callbackParam,releamParam, nil]];
[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *strJson = [NSString stringWithFormat:@"%@",@"{\"products\" : [ {\"qty\" : \"1\",\"product\" : \"404\",\"super_attribute\" : {\"180\" : \"80\", \"92\" : \"17\" } }]}"];
[request setHTTPBody:[strJson dataUsingEncoding:NSUTF8StringEncoding]];
OADataFetcher* dataFetcher = [[OADataFetcher alloc] init];
[dataFetcher fetchDataWithRequest:request delegate:self completionBlock:^(OAServiceTicket *ticket, NSMutableData *data) {
if (data == nil) {
}else{
NSLog(@"header: %@",request.allHTTPHeaderFields);
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON Response: %@",json);
}
}
failedBlock:^{
NSLog(@"Failed");
}];