NodeJS - 有没有办法使用 Google API 获取当前登录的用户?

NodeJS - Is there a way to get the current logged in user with the Google API?

是否可以从 refreshToken、accessToken 或通过类似于 NodeJS 中的Retrieve profile information for a signed-in user 的方法获取当前登录的用户(电子邮件、姓名等)?

您可以使用 userinfo 端点来请求其中的一些信息。

https://openidconnect.googleapis.com/v1/userinfo?access_token=XXXX

一个问题是您需要将 配置文件 范围添加到您的授权请求中,然后您将得到类似这样的内容

{
  "sub": "117475532672775346",
  "name": "Linda Lawton",
  "given_name": "Linda",
  "family_name": "Lawton",
  "picture": "https://lh3.googleusercontent.com/a-/AOh14GhroCYJp2P9xeYeYk1npchBPK-zbtTxzNQo0WAHI20",
  "locale": "en"
}

您还可以通过 google 人 api people.get 了解更多信息。

但是,您无法在不请求电子邮件范围的情况下从 userinfo 端点取回电子邮件,但是在您使用 google 日历范围时,有一个您确实不需要的技巧。

如果您使用 calendar.get 并请求用户的主日历,日历名称就是他们的电子邮件地址。

{
 "kind": "calendar#calendar",
 "etag": "\"Qvsbvz0_YlxYi3Ml2Fd7A\"",
 "id": "xxxxx@gmail.com",
 "summary": "Linda Lawton ",
 "description": "test",
 "timeZone": "Europe/Copenhagen",
 "conferenceProperties": {
  "allowedConferenceSolutionTypes": [
   "hangoutsMeet"
  ]
 }
}