GoogleAuthorizationCodeFlow

时间:2013-09-10 21:40:54

标签: java google-oauth

打电话给:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(
        dataStoreFactory).build();
Credential cred = flow.loadCredential(userIdFromMyApplication);

发现我们的userIdFromMyApplication尚未授权我们使用其Google帐户(即cred为空)我会执行以下操作:

if (cred == null) {
  String url = flow.newAuthorizationUrl().setState("/linkaccount")
     .setRedirectUri("http://myapp.com/oauth2.php").build();
  //redirect them to the url    
}

我的问题是,一旦他们授予我的应用访问权限并且我的应用程序的oauth2重定向网址,我在我的API访问令牌下设置了Google,并且访问code参数集,我该如何关联此{ {1}} code?在构建redirectUrl时,我是否必须在userIdFromMyApplication的调用中设置userIdFromMyApplication?这是一个可行的惯例吗?或者是否有更聪明的方法来确定哪个访问令牌属于哪个用户?

1 个答案:

答案 0 :(得分:1)

将用户ID存储在httpSession中,或将用户ID(理想情况下模糊处理)添加到“state”参数,然后该参数将包含在oauth回调中。我个人使用httpSession。

实际上,第二种方法可能会带来安全风险,因此请使用httpSession。