0

我正在开发一个 dribbble 客户端,但无法使用 AFOAuth2Manager 进行身份验证。我目前正在使用 NXOAuth2Client 但我刚刚遇到 AFOAuth2Manager 所以我想试一试。

使用 NXOAuthClient,我从服务器获取令牌没有问题。但是,当涉及到 AFOAuth2Manager 时,它总是返回错误。

这是我以前用 NXOAuth2Client 初始化的:

    [[NXOAuth2AccountStore sharedStore] setClientID:kDRBClientID
                                             secret:kDRBClientSecret
                                              scope:[NSSet setWithObjects:@"public", @"write", @"comment", @"upload", nil]
                                   authorizationURL:[NSURL URLWithString:kDRBAuthorizationURL]
                                           tokenURL:[NSURL URLWithString:kDRBTokenURL]
                                        redirectURL:[NSURL URLWithString:kDRBRedirectURL]
                                      keyChainGroup:kDRBAccountType
                                     forAccountType:kDRBAccountType];

使用相同的常量,我尝试像这样使用 AFOAuth2Manager 进行初始化:

    NSURL *baseURL = [NSURL URLWithString:kDRBBaseUrl];
    AFOAuth2Manager *OAuth2Manager =
    [[AFOAuth2Manager alloc] initWithBaseURL:baseURL
                                    clientID:kDRBClientID
                                      secret:kDRBClientSecret];
    OAuth2Manager.useHTTPBasicAuthentication = NO;

    NSDictionary *dictionary = @{@"scope" : @"public write",
                                 @"redirect_uri" : kDRBRedirectURL};
     [OAuth2Manager authenticateUsingOAuthWithURLString:@"/oauth/authorize"
                                            parameters:dictionary
                                               success:^(AFOAuthCredential *credential) {
                                                   NSLog(@"Token: %@", credential.accessToken);
                                               }
                                               failure:^(NSError *error) {
                                                   NSLog(@"Error: %@", error);
                                               }];

dribbble的登录流程首先将用户引导到登录页面(即/oauth/authorize页面),用户输入用户名和密码后,它将用户重定向到给定的redirectURL。

我现在的问题是我什至无法进入登录页面。有人可以阐明我做错了什么吗?(我也尝试了来自 AFOAuth2Manager 的不同身份验证方法,但它们都不起作用)。

我对 OAuth2 很陌生,所以请多多包涵。

更新

这是错误日志:

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: client error (422)" UserInfo=0x7fb02ad2de00 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fb02ad6e200> { URL: https://dribbble.com/oauth/authorize } { status code: 422, headers {
    Connection = close;
    "Content-Length" = 47;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Sat, 08 Aug 2015 23:52:20 GMT";
    Server = "nginx/1.4.6 (Ubuntu)";
    Status = "422 Unprocessable Entity";
    "X-Request-Id" = "8489a5f0-206a-48ff-b6e7-81664fc6b6f5";
    "X-Runtime" = "0.006667";
} }

使用 NXOAuth2Client,它将使用我的参数为登录页面准备一个 URL,如下所示:

https://dribbble.com/oauth/authorize?client_id=my_client_id&scope=write%20comment%20upload%20public&redirect_uri=my_redirect_uri&response_type=code

但我注意到使用 AFOAuth2 它会在https://dribbble.com/oauth/authorize引发错误。它不会将我的参数附加到请求网址吗?

4

1 回答 1

1

在发出请求之前添加以下行

OAuth2Manager.responseSerializer = [AFJSONResponseSerializer serializer];

并且,422 Unprocessable Entity - 服务器无法处理包含的指令。该请求格式正确,但由于语义错误而无法执行。

例如,如果 XML 请求正文包含格式正确(即语法正确)但语义错误的 XML 指令,则可能会出现这种错误情况。

于 2015-08-09T04:49:56.827 回答