如何知道使用我的应用程序的谷歌帐户进行身份验证的用户的经过身份验证的电子邮件

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

我正在为我的应用集成谷歌身份验证来创建日历事件。

const {google} = require('googleapis');

const SCOPES = ['https://www.googleapis.com/auth/calendar','https://www.googleapis.com/auth/userinfo.email']

我在身份验证中使用了上述范围并生成了url并发送给了客户端。身份验证成功,我能够接收代码并生成令牌。

但是使用代码,我需要经过身份验证的用户的电子邮件,无论谁登录。是否有任何方式/示例知道如何获取用户的电子邮件?我在谷歌搜索寻找解决方案。但我没有得到那个。帮我了解一下。

node.js google-api google-calendar-api google-oauth google-api-nodejs-client
2个回答
-1
投票

收到令牌后,您需要使用该令牌向userinfo端点发出HTTP请求以获取所需的用户信息。

您可以从https://accounts.google.com/.well-known/openid-configuration上的发现文档中获取该端点的URL。

获取用户信息的当前网址是https://openidconnect.googleapis.com/v1/userinfo


-1
投票

如果您在用户主日历上执行calendar get。所有用户都有主日历

请求

GET https://www.googleapis.com/calendar/v3/calendars/primary

响应

{
 "kind": "calendar#calendar",
 "etag": "\"JWPyaEfg9X1jMhlHCcI4tl2h6uo/AxePbI13h8-KuIOLek\"",
 "id": "[email protected]",
 "summary": "[email protected]",
 "timeZone": "Europe/Copenhagen",
 "conferenceProperties": {
  "allowedConferenceSolutionTypes": [
   "eventHangout"
  ]
 }
}

Id和摘要都将是用户的电子邮件地址。通过这种方式,您将不需要请求https://www.googleapis.com/auth/userinfo.email范围

Node guesss

注意:我不是node.js开发人员,这是一个疯狂的猜测你将不得不解决这个问题

function listEvents(auth) {
  const calendar = google.calendar({version: 'v3', auth});
  calendar.about.get({
    calendarId: 'primary'
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const results = res;
    if (results ) {
      console.log(results.id);
    } else {
      console.log('No upcoming events found.');
    }
  });
© www.soinside.com 2019 - 2024. All rights reserved.