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

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

问题描述

我正在尝试从 Twitter 获取 accessToken 值,以便在我的应用程序中使用它(我需要使用 API v1.1 的身份验证模型来获取 GET statuses/user_timeline);我已经在 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 设置中,读取 Consumer key 和 Consumer secret

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 添加 仅限应用程序 模式.在这种模式下,您可以在没有用户上下文的情况下访问特定的 API 端点,即.您的用户不必进行身份验证.只有应用程序会偶尔进行身份验证以获得承载令牌",然后在每个请求中发送该令牌.

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 中获取状态/用户时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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