我正在开发一个 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引发错误。它不会将我的参数附加到请求网址吗?