我在谷歌应用程序项目上工作,并且在使用 java oauth 库(1.10.1-beta)时遇到了一些困难。
我密切关注:http ://code.google.com/p/google-oauth-java-client/wiki/OAuth2#Authorization_code_flow
问题是我不知道应该从哪里获取 userId 或 userEmail。我知道有 userinfo API,但我实际上是在尝试创建凭据,所以我无法访问 userinfo API AFIAK。
我的应用程序在 localhost 上运行良好(因为 test@example.com 用户总是在那里),但在 google 引擎环境中部署时失败了(NullPointerException user.getUserId())。
// we ask for token because we got authCode
GoogleTokenResponse gTokenResponse = userUtils.getFlow().newTokenRequest(authCode).setRedirectUri(userUtils.getRedirectUri()).execute();
//trying to search for user email / id / whatever
User user = UserServiceFactory.getUserService().getCurrentUser();
//user is null -> nullPointerException is thrown
userUtils.getFlow().createAndStoreCredential(gTokenResponse, user.getUserId());
您能否指出我的用例中的一个缺陷或给我一个提示?我在 SDK 示例、Stackoverflow 和这里搜索了很多,但实现的并不多。
PS:在AuthorizationCodeFlow.createAndStoreCredential (...) 方法中,只有当您为凭据使用持久存储并且是的我正在使用它时,userId 才是强制性的,所以在我的情况下,userId 不能为空。
谢谢。