OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用 AFOAuth2Manager 获取 Twitter REST API 的应用程序凭据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:44
标题: ios - 如何使用 AFOAuth2Manager 获取 Twitter REST API 的应用程序凭据

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

这是我目前所拥有的

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
                                         }];

运行时,它无法通过 403 禁止错误进行身份验证。谁能向我解释我哪里出错了?



Best Answer-推荐答案


这是一个愚蠢的错误——我没有意识到我需要一个参数。为参数传递 @{@"grant_type": @"client_credentials"} 而不是 nil 解决了这个问题。

关于ios - 如何使用 AFOAuth2Manager 获取 Twitter REST API 的应用程序凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30040063/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4