【发布时间】:2013-01-13 11:04:12
【问题描述】:
短篇小说
使用适用于 iOS 的 gtm-oauth2 和 Symfony2 中的 FOSOAuthServerBundle 来实现我自己的 Oauth2 服务器我没有得到回调 finishedSelector 被调用。
这是创建“特殊”视图控制器的地方:
GTMOAuth2ViewControllerTouch * viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:myAuth
authorizationURL:authURL
keychainItemName:nil
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
可能导致finishedSelector,(实现的方法viewController:finishedWithAuth:error)不被调用的原因是什么?
我得到的行为是登录页面已正确呈现,但它充当整个 Web 应用程序的起点,在登录后呈现其余页面,而不是将控件返回给 finishedSelector 和,最后,到必须管理 APP 工作流程延续的视图控制器。
长篇大论
在 Symfony2 中使用 gtm-oauth2 和 FOSOAuthServerBundle,我在尝试使架构捕获登录并从我的 iOS APP 加载经过身份验证的会话时遇到问题。
我正在遵循gtm-oauth2 documentation 中描述的说明,尤其是Signing in to non-Google Services 部分。
按照那里的描述,我有这个方法来创建auth 对象:
- (GTMOAuth2Authentication * ) authForMyAPP
{
//This URL is defined by the individual 3rd party APIs, be sure to read their documentation
NSString * url_string = @"http://myHost/oauth/v2/token";
NSURL * tokenURL = [NSURL URLWithString:url_string];
// We'll make up an arbitrary redirectURI. The controller will watch for
// the server to redirect the web view to this URI, but this URI will not be
// loaded, so it need not be for any actual web page. This needs to match the URI set as the
// redirect URI when configuring the app.
NSString * redirectURI = @"http://myHost/oauth/v2/falseCallBack";
GTMOAuth2Authentication * myAuth;
myAuth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"MyAPP"
tokenURL:tokenURL
redirectURI:redirectURI
clientID:kMyClientID
clientSecret:kMyClientSecret
];
//[myAuth setTokenType:@"Bearer"];
return myAuth;
}
然后,此方法创建“特殊”视图控制器,该控制器应处理登录页面的渲染并在执行登录时返回控制:
- (void)signInToMyAPP()
{
GTMOAuth2Authentication *myAuth = [self authForMyAPP];
NSString* auth_string = @"http://127.0.0.1/~pgbonino/Symfony/web/app.php/oauth/v2/auth";
NSURL * authURL = [NSURL URLWithString:auth_string];
// Display the authentication view
// Creates the "special" viewController passing the `auth` object, the authorization URL and the finishedSelector
GTMOAuth2ViewControllerTouch * viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:myAuth
authorizationURL:authURL
keychainItemName:nil
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[self.navigationController pushViewController:viewController animated:YES];
}
最后,我有了用于finishedSelector 的方法。一旦正确执行登录并且身份验证成功(或出现错误),就应该调用它。这就是我没有完成的事情:
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)myAuth
error:(NSError *)error
{
if (error != nil)
{
// Authentication failed
UIAlertView *alertView = [ [UIAlertView alloc] initWithTitle:@"Authorization Failed"
message:[error localizedDescription]
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alertView show];
}
else
{
// Authentication succeeded
// Assign the access token to the instance property for later use
//self.accessToken = myAuth.accessToken;
[myAuth setShouldAuthorizeAllRequests:YES];
[[Singleton sharedSingleton] setAuth:myAuth];
// Display the access token to the user
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authorization Succeeded"
message:[NSString stringWithFormat:@"Access Token: %@", myAuth.accessToken]
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alertView show];
}
}
这一切都应该在 web 视图中呈现我的登录页面并捕获成功登录以调用 viewController:finishedWithAuth:error 并将会话保存在某个共享对象中。
尽管如此,我得到的行为是我在 web 视图中呈现登录,我正确登录,而不是委托选择器被调用,它只是正常登录应用程序,下一页加载到Web 视图,就像在普通浏览器中一样。所以回调没有被执行。
为什么我没有调用选择器?有什么想法吗?
重要提示:Oauth2 服务器运行良好:如果我从 Safari 调用令牌 URL 和回调 URL,一切正常。令牌和授权码已正确保存在数据库中。
【问题讨论】:
标签: ios symfony callback oauth-2.0 selector