gtm-oauth2访问自己的服务器配置

时间:2013-01-13 11:04:12

标签: ios symfony callback oauth-2.0 selector

简短的故事

使用iOS的gtm-oauth2和Symfony2中的FOSOAuthServerBundle来实现我自己的Oauth2服务器我没有调用callBack finishedSelector

这是创建“特殊”ViewController的地方:

    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工作流程继续的视图控制器。

LONG STORY

在Symfony2中使用gtm-oauth2和FOSOAuthServerBundle时,我遇到了尝试使arquitecture捕获登录并从我的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;
}

然后,此方法创建“特殊”viewController,它应该处理登录页面的呈现并在执行登录时返回控件:

- (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和callBack网址,一切运行良好。令牌和授权码正确保存在数据库中。

1 个答案:

答案 0 :(得分:0)

算了。

只是我。

config.yml中将此参数设置为true时,OAuth2将无法与Symfony2和FOSUserBundle一起使用:

always_use_default_target_path: false
相关问题