我正在制作一个需要 oAuth 1.0 身份验证的应用程序。我可以访问客户提供的消费者密钥和消费者秘密。我曾尝试使用 AFNetworking 来实现,但效果不佳。有人可以建议我一个很好的教程。实际上,未经身份验证的用户出现错误。
问问题
1124 次
1 回答
3
我找到了这个问题的答案。首先,我同样使用了 AFNetworking 和 AFOauth1Client。
AFOAuth1Client * client = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:<your URL>] key:<Your Consumer Key> secret:<Your Consumer Secret>];
[client setOauthAccessMethod:@"POST"];
[client setSignatureMethod:AFHMACSHA1SignatureMethod];
[client setDefaultHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
接下来我为这个客户端创建了一个请求并设置了请求的正文
NSMutableURLRequest * request =[client requestWithMethod:@"POST" path:<URL Path> parameters:nil];
//here path is actually the name of the method
[request setHTTPBody:[<Your String Data> dataUsingEncoding:NSUTF8StringEncoding]];
然后我使用了 AFHttpRequestOperation 和上一步中的请求
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success Print the response body in text
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
//parsing the xml Response
NSXMLParser * parser= [[NSXMLParser alloc] initWithData:responseObject];
[parser setDelegate:self];
[parser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Something Went wrong..
NSLog(@"Error: %@", error);
}];
[operation start];
于 2013-07-07T08:39:23.300 回答