You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Auth返回user对象属性名变动 如何稳定获取id_token

无意义属性名产生原因

  • 这类短字符属性是Google JS SDK经过混淆编译后的产物:Google发布前端库前会用代码压缩工具把内部私有属性名替换成随机短字符,既可以压缩SDK体积,也能提示开发者不要依赖内部属性。
  • 你用到的wc、Zb都属于SDK的私有内部属性,本身不在Google公开的API承诺范围内,每次SDK版本迭代都可能修改这类属性名,写死访问必然会随着SDK更新报错。

稳定获取id_token的方案

Google Auth提供了公开的标准APIgetAuthResponse()来获取授权凭证,无需访问私有属性,调用方式为user.getAuthResponse().id_token,该API有官方兼容性保证,不会随意变更。

修正后的代码示例

将原有写死私有属性的逻辑替换为公开API调用即可:

async authenticate(): Promise<gapi.auth2.GoogleUser> {
    // Initialize gapi if not done yet
    if (!this.gapiSetup) {
      await this.initGoogleAuth();
    }

    // Resolve or reject signin Promise
    return new Promise(async () => {
      await this.authInstance.signIn().then(
        user => {
          this.user = user;
          console.log('this.user: ', this.user);
          
          // 用公开API稳定获取id_token
          const authResponse = user.getAuthResponse();
          this.cookieService.set('jwt', authResponse.id_token, 365);

          this.owlerApiService.getHooterUsingIdTokenFromProvider()
            .subscribe((data: any) => {
              this.userDto = data;            
            },
            error => {
              console.log('error: ', error);
            });
        },
        error => this.error = error);
    });
}

提示:所有第三方SDK未在官方文档明确标注的属性都属于内部属性,不要直接访问,始终使用公开API才能避免版本更新后的兼容问题。

内容的提问来源于stack exchange,提问作者Bob Horn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 01:06:05