我正在使用GAE for java并尝试使用apis从google驱动器中读取文件。我使用了DrEdit的标准实现。我有一个servlet,它使用我的帐户进行身份验证,并使用以下代码段存储凭据:
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;
}
}
保存accessToken& amp;的代码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("/");
}
}
在此之后,我希望GAE每次都能自动检索我的凭据并让我调用驱动器api。但是,我在日志中看到以下消息。所以,我不确定为什么会这样。请注意,我创建的授权网址包括离线范围。
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
强制重新进行身份验证后记录:
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
答案 0 :(得分:1)
“没有过期并使用相同的accessToken”????真?你看过https://developers.google.com/accounts/docs/OAuth2WebServer吗?
答案 1 :(得分:0)
如果您进行了Google两阶段验证,则无论您的Google密码是什么,都不会被接受。您需要生成(在Google上)所谓的特定于应用程序的密码(ASP)。转到https://www.google.com/settings/account并设置ASP,在代码中输入您生成的密码作为密码,然后就完成了。
答案 2 :(得分:0)
尝试替换retrieve(String code)方法,而不是:
Credential cred = buildEmpty();
cred.setRefreshToken(response.getRefreshToken());
cred.setAccessToken(response.getAccessToken());
return cred;
替换为:
Credential cred = buildEmpty();
cred.setFromTokenResponse(response);
return cred;