Twitter OAuth和accessToken到Objective-C中的GET状态/用户时间线 [英] Twitter OAuth and accessToken to GET statuses/user_timeline in Objective-C

查看:115
本文介绍了Twitter OAuth和accessToken到Objective-C中的GET状态/用户时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Twitter获取accessToken值,以在我的应用中使用它(我需要对 GET statuss/user_timeline 使用API​​ v1.1的身份验证模型);我已经在api.twitter.com上注册了我的应用,在项目中导入了 AFOAuth1Client 类,这是简单的代码:

I'm trying to acquire the accessToken value from Twitter for using it in my app (I need to use API v1.1's Authentication Model for GET statuses/user_timeline); I have registered my app on api.twitter.com, imported the AFOAuth1Client classes in the project, and this is the simple code:

- (void)viewDidLoad
{
    self.twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:
                                     [NSURL URLWithString:@"https://api.twitter.com/"] key:@"MYKEY" secret:@"MYSECRETKEY"];
    AFOAuth1Token *accessToken;

    [self.twitterClient acquireOAuthAccessTokenWithPath:@"oauth/request_token" requestToken:accessToken accessMethod:@"GET" success:^(AFOAuth1Token *accessToken) { // I have tried also accessMethod: @"POST"
        NSLog(@"Success: %@", accessToken);
    } failure:^(NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

不幸的XCode给我这个错误:

Unluckly XCode give me this error:

Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x79a14f0 {NSLocalizedRecoverySuggestion=Failed to validate oauth signature and token, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7956390>

这是怎么了?我需要在api.twitter.com上注册应用程序吗?是正确的方法,还是在iOS中使用API​​ v1.1的Twitter身份验证模型获取状态/用户时间线的更简单方法是什么?谢谢!

What is wrong here? DO i need to REGISTER app on api.twitter.com? Is it the right way, or, what is the simpler way to Get statuses/user_timeline using API v1.1's Twitter Authentication Model in iOS? Thank you!

可能的航路点?

1)在dev.twitter.com上注册新应用

1) register a new app on dev.twitter.com

2)在OAuth设置中,阅读消费者密钥"和消费者秘密"

2) in OAuth settings, read Consumer key and Consumer secret

3)设置默认的应用访问类型为读取"?还是读/写?要求访问令牌?在...中使用此值?

3) set default app access type to read? or read/write? ask for access tokens? use this values in.... ?

推荐答案

距离Twitter添加

It's been a few days since Twitter added an application only mode. In this mode, you can access specific API endpoints without user's context, ie. your users don't have to authenticate. Only the application does authenticate once in a while to get a "bearer token" which is then sent in every request.

以下代码获取承载令牌,然后为@barackobama检索公共时间轴.

The following code gets a bearer token and then retrieves the public timeline for @barackobama.

STTwitterAPI *twitter = [STTwitterAPI twitterAPIApplicationOnlyWithConsumerKey:@""
                                                                consumerSecret:@""];

[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

    NSLog(@"Access granted with %@", bearerToken);

    [twitter getUserTimelineWithScreenName:@"barackobama" successBlock:^(NSArray *statuses) {
        NSLog(@"-- statuses: %@", statuses);
    } errorBlock:^(NSError *error) {
        NSLog(@"-- error: %@", error);
    }];

} errorBlock:^(NSError *error) {
    NSLog(@"-- error %@", error);
}];

有关有效示例,请参见 STTwitter iOS演示项目(使用您自己的使用者密钥/秘密).

See STTwitter iOS demo project for a working example (use you own consumer key / secret).

这篇关于Twitter OAuth和accessToken到Objective-C中的GET状态/用户时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆