【问题标题】:Oauth user authentication from iOS device to google app engine从 iOS 设备到谷歌应用引擎的 Oauth 用户身份验证
【发布时间】:2012-03-16 08:39:25
【问题描述】:

我正在开发一个 iOS 应用程序,该应用程序需要将数据获取/放入我也在开发的谷歌应用引擎服务器应用程序。 iOS 应用程序的每个用户都将使用他们的谷歌身份来访问应用程序的服务器部分。我正在尝试使用漂亮的gtm-oauth library。我已使用 Google 的服务 register my domain 来获取我的 OAuth 消费者密钥和 OAuth 消费者密钥。

当我为用户创建代码以访问 Google 的联系人列表时,它工作正常,但我无法让它针对我的应用引擎应用程序工作。当我尝试时,在身份验证控制器视图中出现错误“您请求的服务尚不可用。请在 30 秒后重试”。在应用引擎控制台中,我看到对 /_ah/OAuthGetAccessToken 的请求失败(我没有在该路径提供任何内容)。

这是我的代码:

-(IBAction)authButtonClicked: (id) sender {
    [GTMHTTPFetcher setLoggingEnabled:YES];
    NSURL *requestURL = [NSURL URLWithString:@"https://mysite.appspot.com/_ah/OAuthGetRequestToken"];
    NSURL *accessURL = [NSURL URLWithString:@"https:/mysite.appspot.com/_ah/OAuthAuthorizeToken"];
    NSURL *authorizeURL = [NSURL URLWithString:@"https://mysite.appspot.com/_ah/OAuthGetAccessToken"];
    NSString *scope = @"http://mysite.appspot.com/";
    GTMOAuthAuthentication *auth = [self myCustomAuth];
    GTMOAuthViewControllerTouch *viewController;
    viewController = [[GTMOAuthViewControllerTouch alloc] initWithScope:scope
                                                            language:nil
                                                     requestTokenURL:requestURL
                                                   authorizeTokenURL:authorizeURL
                                                      accessTokenURL:accessURL
                                                      authentication:auth
                                                              appServiceName:@"My Service"
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    [[self navigationController] pushViewController:viewController animated:YES];
}

- (GTMOAuthAuthentication *)myCustomAuth {
    NSString *myConsumerKey = @"mysite.appspot.com";    // from google registration
    NSString *myConsumerSecret = @"xxxxxxxxxxxxxxx";   //  from google registration
    GTMOAuthAuthentication *auth;
    auth = [[GTMOAuthAuthentication alloc]             initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                                                        consumerKey:myConsumerKey
                                                         privateKey:myConsumerSecret];
    auth.serviceProvider = @"Custom Auth Service";
    return auth;
}

我使用的网址是否正确?范围是否正确?什么会导致该消息?

【问题讨论】:

    标签: ios google-app-engine oauth


    【解决方案1】:

    我在代码中看到几个错误和一个缺少的方法调用:
    首先,这个网址是错误的:

    NSURL *accessURL = [NSURL URLWithString:@"https:/mysite.appspot.com/_ah/OAuthAuthorizeToken"];
    

    它应该指向:

    NSURL *accessURL = [NSURL URLWithString:@"https://mysite.appspot.com/_ah/OAuthGetAccessToken"];
    

    第二,这个url也是错的:

    NSURL *authorizeURL = [NSURL URLWithString:@"https://mysite.appspot.com/_ah/OAuthGetAccessToken"];
    

    它应该指向这个:

    NSURL *authorizeURL = [NSURL URLWithString:@"https://mysite.appspot.com/_ah/OAuthAuthorizeToken"];
    

    最后在 myCustomAuth 方法的末尾和 return 之前添加这行代码:

    [auth setCallback:@"http://mysite.appspot.com/_my_callback"];
    

    最后一部分回调url指向哪里无关紧要,因为它不会在iOS设备的safari浏览器上加载。
    希望对您有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      相关资源
      最近更新 更多