OAuth2 获取 tokeninfo 的 API(https://www.googleapis.com/oauth2/v3/tokeninfo)偶尔会遇到缺少 “sub” 字段的问题

The OAuth2 API for obtaining tokeninfo (https://www.googleapis.com/oauth2/v3/tokeninfo) occasionally experiences the issue of missing the "sub" field

提问人:张新尧 提问时间:10/10/2023 最后编辑:张新尧 更新时间:10/10/2023 访问量:37

问:

我们最近在验证 Google 令牌时遇到了一些失败。当从 OAuth2 API (https://www.googleapis.com/oauth2/v3/tokeninfo) 收到的响应不包含“sub”字段时,会发生所有这些故障。请协助我们解决此问题。

这是我们收到的回复:

{
 "aud": "919759----------apps.googleusercontent.com",
 "expires_in": "3598",
 "access_type": "offline", 
 "exp": "1696667490",
 "azp":
 "919759---------apps.googleusercontent.com",
 "scope": "https://www.googleapis.com/auth/games_lite"
}

我们在 Android 上使用 Google Play 游戏服务登录并获取 AuthCode。

GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(this);
gamesSignInClient
  .requestServerSideAccess(OAUTH_2_WEB_CLIENT_ID, /* forceRefreshToken= */ false)
  .addOnCompleteListener( task -> {
    if (task.isSuccessful()) {
      String serverAuthToken = task.getResult();
      // Send authentication code to the backend game server to be
      // exchanged for an access token and used to verify the player
      // via the Play Games Services REST APIs.
    } else {
      // Failed to retrieve authentication code.
    }
});

然后,我们将 AuthCode 发送到服务器以获取访问令牌。

最后,我们使用 OAuth2 API (https://www.googleapis.com/oauth2/v3/tokeninfo) 检索令牌信息,典型响应如下:

{
 "azp": "1615072----------apps.googleusercontent.com",
 "aud": "1615072----------apps.googleusercontent.com",
 "sub": "1020----------3970",
 "scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/games_lite https://www.googleapis.com/auth/userinfo.profile openid",
 "exp": "1696654678",
 "expires_in": "3557",
 "email": "[email protected]",
 "email_verified": "true",
 "access_type": "offline"}

启用对 Google Play 游戏服务的服务器端访问

oauth-2.0 范围 google-play-games

评论


答: 暂无答案