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