2012-08-10 122 views
2

我在谷歌应用程序项目上工作,我正在努力与java oauth库(1.10.1-beta)。Oauth:从哪里获取AuthorizationCodeFlow的userId。 createAndStoreCredential?

我紧跟:http://code.google.com/p/google-oauth-java-client/wiki/OAuth2#Authorization_code_flow

问题是,我不从那里我应该得到用户id或USEREMAIL知道。我知道有userinfo API,但我实际上是在创建凭证,所以我无法访问userinfo API AFIAK。

我的应用程序在localhost上很好地工作(因为[email protected]用户总是在那里),但在谷歌引擎环境中部署时失败(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不能为null。

谢谢。

回答

0

在识别用户(身份验证)之前,您正在进行OAUTH(授权)。您必须将用户重定向到登录页面时未登录他:

UserServiceFactory.getUserService().getCurrentUser() == null 

你做到这一点的用户重定向到loginUrl:下一次用户到达您的应用程序

String loginUrl = userService.createLoginURL(request.getOriginalRef().toString()); 

,他将登录,并且您可以询问userId。

+0

但是,如果在用户授予应用程序访问权后运行代码,那么应用程序必须知道谁登录了Google帐户。 – Martin85 2012-08-12 20:36:56

+0

if UserServiceFactory.getUserService()。getCurrentUser()== null 那么恐怕用户没有登录他的谷歌账户 – koma 2012-08-13 07:33:02