OAuth登录后获取用户信息

时间:2012-07-31 20:17:40

标签: iphone objective-c ios twitter twitter-oauth

我想获取用户的信息。在OAuth之后,我发送此请求:

 NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/account/verify_credentials.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"Public Timeline: %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"error = %@", error);
    NSLog(@"json = %@", JSON);}];
[operation start];

但收到错误:{"error":"Could not authenticate you.","request":"\/1\/account\/verify_credentials.json"}为什么即使在登录后我也无法访问信息?

1 个答案:

答案 0 :(得分:2)

您尚未将OAuth令牌附加到请求。您只是发送带有该URL字符串的请求。

您可以查看AFOAuth2Client,它可以为AFNetworking提供一个简洁的扩展,以便在Twitter上执行授权请求。

或者,您可以使用内置的iOS 5+ Twitter API,I wrote an answer作为另一个SO帖子,向您展示如何完成此操作 - 这非常简单:)

如果您需要从头开始实施OAuth,请参阅:https://github.com/bengottlieb/Twitter-OAuth-iPhone/

祝你好运!