【问题标题】:How to validate google oauth2 token from java code如何从 Java 代码验证 google oauth2 令牌
【发布时间】:2012-07-04 22:37:31
【问题描述】:

我有一个 android 应用程序和 web 服务器一起工作。现在我希望用户从 android 应用程序通过 google 登录(或使用 android 上的 google 帐户之一)。然后,andriod 应用程序通过服务调用将令牌传递给我的网络服务器......在这里我无法意识到如何从具有该令牌的谷歌获取用户电子邮件或个人资料数据。

我可以在浏览器中拨打这样的电话:https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={accessToken}

但是如何使用谷歌图书馆做到这一点?使用什么库等等?

【问题讨论】:

    标签: oauth token


    【解决方案1】:

    根据您尝试使用的服务,只需在Google's Client Libraries 中选择正确的一项,然后查看Google+ Sample

    所有 API 的前半部分应该基本相同。要获取用户信息,您需要oauth2 library,然后执行以下操作(取自this example):

    // Set up the HTTP transport and JSON factory
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    
    // Set up OAuth 2.0 access of protected resources
    // using the refresh and access tokens, automatically
    // refreshing the access token when it expires
    GoogleAccessProtectedResource requestInitializer =
        new GoogleAccessProtectedResource(accessToken, httpTransport,
        jsonFactory, clientId, clientSecret, refreshToken);
    
    // set up global Oauth2 instance
    Oauth2 oauth2 = new Oauth2.Builder(httpTransport, jsonFactory, requestInitializer)
        .setApplicationName("Google-OAuth2Sample/1.0").build();
    
    Userinfo userinfo = oauth2.userinfo().get().execute();
    
    【解决方案2】:

    我使用了这里的代码示例:https://github.com/googleplus/gplus-verifytoken-java,这似乎是最新的。代码最终是这样的:

    GoogleCredential credential = new GoogleCredential().setAccessToken( accessToken );
    Oauth2 oauth2 = new Oauth2.Builder(
        UrlFetchTransport.getDefaultInstance(),
        JacksonFactory.getDefaultInstance(), credential )
        .build();
    Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken( accessToken ).execute();
    
    // ... check tokeninfo expiry and issued to etc ...
    

    【讨论】:

      【解决方案3】:

      您将需要这个库google-api-client JAR。这是工作代码如何做到这一点。随身携带您的 ID_TOKEN 和 CLIENT_ID。

      String jwt = "YOUR_ID_TOKEN";
      GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(),new GsonFactory())
                              .setAudience(Collections.singletonList("YOUR_CLIENT_ID"))
                              .build();
              
          GoogleIdToken idToken;
              
          try {
                idToken = verifier.verify(jwt);
              } catch (GeneralSecurityException e) {
                 throw new RuntimeException("Cannot verify the ID_TOKEN send :" + e.getMessage());
              }
              
          if(idToken == null){
                throw new RuntimeException("Failed to verify the ID_TOKEN send");
              }
              
          String username =  idToken.getPayload().getEmail();
      

      【讨论】:

        猜你喜欢
        • 2015-08-27
        • 2017-01-12
        • 2021-02-10
        • 2015-07-07
        • 2015-11-11
        • 1970-01-01
        • 2012-04-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多