有没有办法在数据库中将 link Google 身份令牌发送给我公司的用户?

Is there a way to link the Google Identity Token to my company user in the database?

我正在 google 项目上设置一个操作,该项目使用 OAuth 和 Google 登录链接类型。

以前,我使用在每个请求中发送的 userId 来查找数据库中的用户,以查看是否有可用的访问令牌和刷新令牌。但由于 userId 已被弃用,我正在寻找替代方案。

用户启动 his/her 对话框,然后碰到这段代码:

app.intent('Give Color', async (conv, { color }) => {
  conv.data[Fields.COLOR] = color;
  if (conv.user.ref) {
    await conv.user.ref.set({ [Fields.COLOR]: color });
    conv.close(`I got ${color} as your favorite color.`);
    return conv.close('Since you are signed in, I\'ll remember it next time.');
  }
  return conv.ask(new SignIn(`To save ${color} as your favorite color for next time`));
});

用户选择正确 Google account.Then 我的 /token 端点的 "To continue, link Test App to your Google Account" 在包含 Google ID 令牌的 OAuth 服务器上被调用(断言)其中包含所有用户数据。我对其进行解码,如果 "sub" 已经存在,请检查数据库,然后抛出以下异常:

return res.status(401).send({ error: 'user_not_found' });

然后正常的 OAuth 程序开始,我将令牌发送到 Google。旁注:这是我自己用 NodeJS 编写的 OAuth 服务器。我确定访问令牌和刷新令牌已传送到 Google。

令牌交付后,我收到新的操作请求:

app.intent('Get Sign In', async (conv, params, signin) => {
  if (signin.status !== 'OK') {
    return conv.close('Let\'s try again next time.');
  }
  const color = conv.data[Fields.COLOR];
  await conv.user.ref.set({ [Fields.COLOR]: color });
  return conv.close(`I saved ${color} as your favorite color. `
    + 'Since you are signed in, I\'ll remember it next time.');
});

signin.status 的值为 "OK"。但是 conv.user 对象不应该包含 Google ID 令牌,这样我就可以将访问和刷新令牌与 Google ID 令牌中的 "sub" 一起存储在我的数据库中?还是我弄错了什么?

conv.user 的内容如下所示:

User {raw: Object, storage: Object, _id: undefined, locale: "en-BE", verification: "VERIFIED", …}
_id: undefined
[[StableObjectId]]: 7
access: Access {token: "ACCT-ATlbRmcpMI545WJFssRSlK1Jcza46NIB"}
entitlements: Array(0) []
id: undefined
last: Last {seen: Thu Aug 08 2019 10:53:17 GMT+0200 (Central Europea…}
locale: "en-BE"
name: Name {display: undefined, family: undefined, given: undefined}
permissions: Array(0) []
profile: Profile {token: undefined}
raw: Object {accessToken: "ACCT-ATlbRmcpMI545WJFssRSlK1Jcza46NIB", locale: "en-BE", lastSeen: "2019-08-08T08:53:17Z", …}
storage: Object {}
verification: "VERIFIED"
__proto__: Object {constructor: , _serialize: , _verifyProfile: , …}
conv.user.id is *DEPRECATED*: Use conv.user.storage to store data instead

它不会包含用户的 Google ID,因为用户尚未授权。

他们授权的是您要求他们通过您的 OAuth 服务器授权的任何内容。

因此,您会在 conv.user.access 中看到您的服务器已发送给智能助理的访问令牌,然后您可以使用此令牌来查找用户在您的数据库中的身份并采取相应的操作。

如果您特别想要他们的 Google ID,则需要确保他们在与您的操作相同的项目上使用 Google 登录(通过语音、移动应用程序) ,或网络应用程序)。

如果您只需要一个 ID 以便稍后可以看到此用户 returns,您可以使用从 Google 登录获得的 Google ID,或者直接生成一个 ID 并将其存储在 conv.user.storage.

因为我只想有一个 ID,所以我将使用这个:

如果您只需要一个 ID 以便稍后可以看到此用户 returns,您可以使用从 Google 登录获得的 Google ID,或者直接生成一个 ID 并将其存储在 conv.user.storage.

谢谢!