如何使用AFOAuth2Manager获取Twitter REST API的应用程序凭据

时间:2021-03-13 03:48:58

I'm trying to access a single part of the Twitter API, without logging in a user account (application-only authentication is what I want), so I don't want to add some framework just to perform this one operation. I'm trying to do it with AFNetworking instead based on the description here: https://dev.twitter.com/oauth/reference/post/oauth2/token .

我正在尝试访问Twitter API的单个部分,而无需登录用户帐户(仅应用程序身份验证是我想要的),所以我不想添加一些框架来执行这一操作。我正在尝试使用AFNetworking而不是基于此处的描述:https://dev.twitter.com/oauth/reference/post/oauth2/token。

Here's what I have so far

这是我到目前为止所拥有的

AFOAuth2Manager *authManager = [[AFOAuth2Manager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/"]
                                                               clientID:TwitterAPIKey
                                                                 secret:TwitterSecret];
[authManager authenticateUsingOAuthWithURLString:@"oauth2/token"
                                      parameters:nil
                                         success:^(AFOAuthCredential *credential) {
                                           AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:TwitterBaseURL]];
                                           [manager.requestSerializer setAuthorizationHeaderFieldWithCredential:credential];
                                           //do some stuff
                                         }
                                         failure:^(NSError *error) {
                                           //handle the failure, where I'm currently ending up
                                         }];

When run, this fails to authenticate with a 403 forbidden error. Can anyone explain to me where I'm going wrong?

运行时,无法通过403禁止错误进行身份验证。任何人都可以向我解释我哪里出错了?

1 个解决方案

#1


It was a stupid mistake -- I didn't realize that I needed a parameter. passing @{@"grant_type": @"client_credentials"} for parameters instead of nil fixed this issue.

这是一个愚蠢的错误 - 我没有意识到我需要一个参数。传递@ {@“grant_type”:@“client_credentials”}参数而不是nil修复此问题。

#1


It was a stupid mistake -- I didn't realize that I needed a parameter. passing @{@"grant_type": @"client_credentials"} for parameters instead of nil fixed this issue.

这是一个愚蠢的错误 - 我没有意识到我需要一个参数。传递@ {@“grant_type”:@“client_credentials”}参数而不是nil修复此问题。