如何从 NodeJS 中的 Google API 的 oAuth2Client 获取“sub”或 id_token

问题描述 投票:0回答:1

我正在使用 oAuth2Client 访问用户的 Google Analytics 属性。他们的刷新令牌被保存以供将来使用。但是,由于用户可以添加多个属性,因此我需要将刷新令牌分配给用户的 Google 帐户,而不是 GA 属性。看来我应该能够通过“子”字符串来识别 Google 帐户。这应该可以在 token_id 或凭证中访问。我已经尝试过这两种方法 - token_id 未在令牌对象中返回,并且凭据返回空。这是我尝试过的:

import { google } from 'googleapis';
...
//POST token request to Google using received code
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
const tokenRes = await oAuth2Client.getToken(code);

console.log(JSON.stringify(oAuth2Client.credentials));//{}
console.log(JSON.stringify(tokenRes.tokens));//{'access_token':'abc123', 'refresh_token':'1//abc123', etcButNoIdToken}
node.js google-api google-oauth google-analytics-api google-api-nodejs-client
1个回答
0
投票

谷歌分析范围不会为您提供任何个人资料信息。

您应该在请求的同时请求配置文件范围,然后您将能够识别当前的授权用户。

提示:id 令牌来自登录或身份验证。不是Oauth2同意或授权。

© www.soinside.com 2019 - 2024. All rights reserved.