java - 如何将 google 身份验证 token (在 iOS 中获得)传递到 google-app-engine Web servlet?

标签 java objective-c ios google-app-engine oauth-2.0

我是网络服务器编程的新手,所以也许我的做法是错误的,但这就是我想要做的:

  1. 用户使用 native iOS 应用程序针对服务器“https://www.googleapis.com/auth/plus.me”进行身份验证。我正在使用, GTMOAuth2 classes来处理授权。我已成功检索到有效 token ,因此这部分可以正常工作。

  2. 我现在尝试使用在步骤 1 中获得的身份验证 token ,以经过身份验证的用户身份将 JSON 数据 POST 到 google-app-engine Web servlet。以下是我正在使用的代码:

    - (void)postDictionary:(NSMutableDictionary *)dict toURL:(NSMutableString *)urlString     withAuthToken:(GTMOAuth2Authentication *)authToken delegate:(id)connectDelegate
    {
    
    if( ![NSJSONSerialization isValidJSONObject:dict] )
    {
        NSLog(@"JSON Data is Invalid!");
        return;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0 error:nil];
    NSMutableString *serverURL = [[NSMutableString alloc] initWithString:urlString];
    //DEBUG
    NSLog(@"sent URL: %@", serverURL);
    
    NSURL *url = [[NSURL alloc] initWithString:serverURL];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    
    // Execute URL Asynchronously
    // Make sure we "Post" the data to the server
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:jsonData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
    [request setValue:[NSString stringWithFormat:@"%d",
                   [jsonData length]] forHTTPHeaderField:@"Content-Length"];
    
    // We need to send the authentication information to the server.
    [request setValue:CLIENT_ID forHTTPHeaderField:@"client_id"];
    [request setValue:CLIENT_SECRET forHTTPHeaderField:@"client_secret"];
    [request setValue:authToken.accessToken forHTTPHeaderField:@"Authorization: GoogleLogin auth"];
    // Make sure we can authenticate on HTTP instead of just HTTPS
    authToken.shouldAuthorizeAllRequests=YES;   // USED FOR TESTING ON LOCALHOST ONLY!!!
    
    
    // Let's Try the Fetcher method...
    GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request];
    [myFetcher setAuthorizer:authToken];
    [myFetcher beginFetchWithDelegate:self
                didFinishSelector:@selector(myFetcher:finishedWithData:error:)];
     return;
    }
    

目前我尝试发布的服务器正在“http://localhost:8888/mywebapp”上运行。

  1. 在 Web 服务器上,我尝试使用以下 java 代码对用户进行身份验证:

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    // Perform Authentication...
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    
    if( user != null ) {
        System.out.println("User: "+user.getNickname());
    } else {
        sendError( "You must login first!",  resp );
        return;
    }
    ...
    

UserService 从不表明用户已登录。我尝试了几种不同的方法,但没有取得任何成功。我的应用程序刚刚收到“您必须先登录!”错误信息。有什么想法吗?

最佳答案

我解决了我的问题。

  1. 我必须切换到 OAuth 1 协议(protocol)而不是 OAuth 2 协议(protocol)。根据我阅读的文档,Google App Engine 尚不支持 OAuth 2(有人知道不同吗?)。所以我使用了库GTM-OAUTH而不是我的 iOS 应用程序中的 GTM-OAUTH2 库。我使用“OAuthSampleTouch”示例作为使用该库的基础。

  2. 我需要使用 https://accounts.google.com/managedomains 向 Google 注册我的 google-app-engine 网址。 这给了我一个 ConsumerKey 和 ConsumerSecret,我可以在调用 GTM-OAUTH 库时在我的 iPhone 应用程序中使用。

  3. 我修改了 GAE 服务器代码以使用 OAUTH token :

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    // Perform Authentication...
      User user = null;
      try {
      OAuthService oathService = OAuthServiceFactory.getOAuthService();
      user = oathService.getCurrentUser();
      System.out.println("Logged in! YAY!!!");
      } catch (OAuthRequestException e) {
         System.out.println("Authentication Failed! "+e);
      }
      ...
    

我还将 token 附加到 URL 中,我不确定这是否是必需的...

示例: URL="https://my-app.appspot.com/myservlet?token=XXXXXXXXXXXXXXX"

关于java - 如何将 google 身份验证 token (在 iOS 中获得)传递到 google-app-engine Web servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14132795/

相关文章:

Java:如何在 session 中记录任何内容?

objective-c - 如何将数字范围从 0-200 转换为 1-5 范围

objective-c - Obj-C,对此 xcode 警告感到困惑,警告 : 'BNColor' may not respond to '+beInit' ?

java - 不出现通知

java - ECDSA 使用 BouncyCaSTLe 签名并使用 Crypto++ 验证

java - 可被 3 整除的数字递归

ios - 将 float 数组的数组转换为 UnsafeMutablePointer<UnsafeMutablePointer<Float>>

iphone - 如何通过传递手机号码作为参数从地址簿中获取人员的联系人图像

ios - 自定义 UIView 的边框绘制不流畅

ios - Xcode 6.4 Scheme 选项在 El Capitan Beta 中消失