我试图按如下方式访问Java Servlet中的App Engine用户个人资料。
String scope = "https://www.googleapis.com/auth/userinfo.email profile";
OAuthService oathService = OAuthServiceFactory.getOAuthService();
User user = oathService.getCurrentUser(scope);
每当访问用户数据时,都会引发OAuthRequestException。 相同的代码在开发模式下有效,但在部署模式下无效。
我不明白为什么会引发错误。 据我了解,所需的权限如在线文档中所述(我想是!)可用。
当请求不是有效的OAuth请求时,将抛出OAuthRequestException。 此链接有一些示例,例如:
String scope = "https://www.googleapis.com/auth/userinfo.email";
try {
user = oauth.getCurrentUser(scope);
} catch (OAuthRequestException e) {
log("Impossible to authenticate the request from the image resizer, oauth request exception: "
+ e.getMessage());
resp.setStatus(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
return;
}
通过调用UserService
获得当前User
可以解决该错误。 但是对于开发模式而言,这是行不通的。 可能会有Google App Engine小组的人可以解释!
UserService userService = UserServiceFactory.getUserService();
user = userService.getCurrentUser();
if (user == null) {
res.sendRedirect(userService.createLoginURL(req.getRequestURI()));
return;
}