2

I am using GAE for java and trying to read files from google drive using apis. I used the standard implementation from DrEdit. I have a servlet which authenticate with my account and store the credentials using the following snippet:

 protected void handleCallbackIfRequired(HttpServletRequest req,
      HttpServletResponse resp) throws IOException {
    String code = req.getParameter("code");
System.out.println("Code:" + code);

if (code != null) {
  // retrieve new credentials with code
  Credential credential = credentialManager.retrieve(code);

credentialManager.save(id, credential);
System.out.println("access token:" + credential.getAccessToken());
System.out.println("refresh token:" + credential.getRefreshToken());
  resp.sendRedirect("/");
}

}

  public Credential retrieve(String code) {
    System.out.println("Passed code to retrieve is:" + code);
      try {
      GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(
          transport,
          jsonFactory,
          clientSecrets.getWeb().getClientId(),
          clientSecrets.getWeb().getClientSecret(),
          code,
          clientSecrets.getWeb().getRedirectUris().get(0)).execute();

      Credential cred = buildEmpty();
      cred.setRefreshToken(response.getRefreshToken());
      cred.setAccessToken(response.getAccessToken());
      return cred;

    } catch (IOException e) {
      new RuntimeException("An unknown problem occured while retrieving token");
    }
    return null;
  }
}

Code for saving the accessToken & refreshToken:

protected void handleCallbackIfRequired(HttpServletRequest req,
      HttpServletResponse resp) throws IOException {
    String code = req.getParameter("code");
    System.out.println("Code:" + code);

    if (code != null) {
      // retrieve new credentials with code
      Credential credential = credentialManager.retrieve(code);

    credentialManager.save(id, credential);
    System.out.println("access token:" + credential.getAccessToken());
    System.out.println("refresh token:" + credential.getRefreshToken());
      resp.sendRedirect("/");
    }
  }

After this I am expecting the GAE to retrieve my credentials automatically each time and let me call the drive apis. However, I am seeing below message in my logs. So, I am not sure why that is happening. Please note that the authorization url i created includes offline scope.

  "code" : 401,
    "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
    } ],
    "message" : "Invalid Credentials"
    }

Logs after forcing a fresh reauthentication:

2013-09-08 22:51:28.512
[s~sakshumweb-hrd/3.370083570818804963].<stdout>: Code:4/jYpKc6Mit2_0OTgR7dhpve0YGQCG.UoLMhsf6Fd4SEnp6UAPFm0GoUpACggI

I 2013-09-08 22:51:28.512
[s~sakshumweb-hrd/3.370083570818804963].<stdout>: Passed code to retrieve is:4/jYpKc6Mit2_0OTgR7dhpve0YGQCG.UoLMhsf6Fd4SEnp6UAPFm0GoUpACggI

I 2013-09-08 22:51:29.423
[s~sakshumweb-hrd/3.370083570818804963].<stdout>: access token:ya29.AHES6ZS3x8fPpq5G9YHmIvFHE098izZ0zyn7BCnZRDhYf3zdA-qNDtw

I 2013-09-08 22:51:29.424
[s~sakshumweb-hrd/3.370083570818804963].<stdout>: refresh token:null

I 2013-09-08 22:51:29.450
[s~sakshumweb-hrd/3.370083570818804963].<stdout>: authorization url:https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=auto&client_id=955443778501.apps.googleusercontent.com&redirect_uri=http://www.sakshum.org/GoogleOauth&response_type=code&scope=https://www.googleapis.com/auth/drive.readonly%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile
4

3 回答 3

1

“没有过期并使用相同的accessToken”????真的吗?你读过https://developers.google.com/accounts/docs/OAuth2WebServer吗?

于 2013-09-10T03:36:04.937 回答
0

如果您启用了 Google 两阶段验证,那么无论您的 Google 密码是什么,都不会被接受。您需要生成(在 Google 上)所谓的应用程序特定密码 (ASP)。转到https://www.google.com/settings/account并设置一个 ASP,在您的代码中输入您生成的密码作为密码,您就完成了。

于 2013-11-24T22:16:25.833 回答
0

尝试在检索(字符串代码)方法中替换,而不是:

Credential cred = buildEmpty();
cred.setRefreshToken(response.getRefreshToken());
cred.setAccessToken(response.getAccessToken());
return cred;

用。。。来代替:

Credential cred = buildEmpty();
cred.setFromTokenResponse(response);
return cred;
于 2014-05-16T20:11:04.667 回答