【问题标题】:Google Calendar API - Get user's email address to add them as attendeeGoogle Calendar API - 获取用户的电子邮件地址以将他们添加为与会者
【发布时间】:2022-11-05 05:14:34
【问题描述】:

我有一个带有 Oauth 2.0 的 Google Calendar API 设置。我的网络应用程序有教师和学生。老师的创作活动,学生可以参加。我已经为所有用户创建了功能,以将他们的 Google 帐户与日历 API 的权限连接起来,教师可以 create events 并成功删除它们,该事件已从他们的 primary 日历中添加和删除。

为此,我不需要老师的帐户电子邮件地址。当他们授权我的应用程序 API 访问他们的帐户时,我得到一个具有 CALENDAR_EVENTS 范围的令牌,我可以单独使用它在他们的日历中创建一个事件。令牌内容是这样的:

{
    "access_token":"[redacted]",
    "expires_in":3599,
    "refresh_token":"[redacted]",
    "scope":"https:\/\/www.googleapis.com\/auth\/calendar.events",
    "token_type":"Bearer",
    "created":1656203897
}

我也有来自具有类似令牌的学生的 Oauth 2 授权。当学生加入活动时,我想update the event 并将该学生添加为与会者。他们在我的应用上使用的帐户的电子邮件地址可能与他们为我的应用授权 Oauth 2 时使用的 Google 帐户不匹配。所以,我需要一种方法来获取他们授权的帐户的电子邮件地址。

这是将授权用户添加为与会者的正确过程吗?我四处寻找获取用户帐户信息的方法,发现大多是过时的信息。我更喜欢使用 PHP Google SDK 中的 Google_Client() 对象,甚至更好的是 Google_Service_Calendar() 对象。

如前所述,我只要求Google_Service_Calendar::CALENDAR_EVENTS 范围。我是否需要添加更多范围才能获取此信息?我也找到了这个Google Identify documentation,但不确定我是否需要这个。

我尝试使用另一种方法来尝试使用 GET 请求的 oauth2 API:

<?php   
    $resp = file_get_contents("https://www.googleapis.com/oauth2/v3/userinfo?access_token=[redacted]");
    print($resp);
?>

    Warning: file_get_contents(https://www.googleapis.com/oauth2/v3/userinfo?access_token=[redacted]): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized in /path/to/api_user.php on line 4 

编辑:

阅读第一个答案后,我阅读了documentation here 并使用了文档中提供的 PHP 代码:

require_once 'vendor/autoload.php';

// Get $id_token via HTTPS POST.

$client = new Google_Client(['client_id' => $CLIENT_ID]);  // Specify the CLIENT_ID of the app that accesses the backend
$payload = $client->verifyIdToken($id_token);
if ($payload) {
  $userid = $payload['sub'];
  // If request specified a G Suite domain:
  //$domain = $payload['hd'];
} else {
  // Invalid ID token
}

代码的第一部分工作正常,我可以使用 client_id 获取 $client 对象。但是,它没有说明从哪里获得 $id_token。我看到from this post id_token 是 Oauth2 响应中的一个字段,但我的响应 JSON 不包含此字段。

【问题讨论】:

    标签: php google-oauth google-calendar-api


    【解决方案1】:

    要获取您用户的电子邮件地址,您可以在他们的用户授权您的应用程序使用 Google Calendar API 时请求它。您只需在setScopes() 中包含范围。下面是包含该范围的 PHP 代码示例。请注意,setScopes() 有一个包含 2 个作用域的数组。

    function sendUserToGoogleCalendarAPIAuthorization()
      {
        // Require the Google API SDK
        require_once 'vendor/Google/autoload.php';
        // Setup the client object
        $client = new Google_Client();
        $client->setApplicationName('Your App');
        // Set scope with added user email scope
        $client->setScopes([Google_Service_Calendar::CALENDAR_EVENTS, 'https://www.googleapis.com/auth/userinfo.email']);
        $client->setAuthConfig('path/to/your/apps/oauth2/client_id.json');
        $client->setAccessType('offline');
        $client->setPrompt('select_account consent');
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        // Header to the authUrl to get user to authorize
        header('Location: '.$authUrl);
      }
    

    在那之后,您将无法使用 Google Calendar API 获取用户的电子邮件。

    【讨论】:

      【解决方案2】:

      您需要添加 https://www.googleapis.com/auth/userinfo.email 范围

      • 使用此范围,您将能够检索用户的电子邮件。

      【讨论】:

      • 因此,即使用户授权了日历 API,我也无法使用 https:\www.googleapis.comuthcalendar.events 范围来执行此操作?
      • 我不这么认为,但googleapis.com/auth/userinfo.email 有什么问题?这是一个非敏感范围。
      • @raw-binhood。我看到了一条关于您询问 ID 令牌的通知。我本来打算解释一下,并建议你另一种解决方案,但你似乎不再感兴趣了?
      • 我提出了与 PHP 相关的答案,并在获得授权时将 userinfo.email 范围添加到请求的范围中,这是对 OP 的直接答案。授权请求现在也返回该范围。唯一剩下的问题是,当我这样做时,来自 Google 的授权页面会为日历范围添加一个复选框,而且它对用户不友好:stackoverflow.com/questions/72793032/…
      • 我提供的答案是针对您拥有客户端和服务器端的浏览器集成。没想到这不是你的本意。至于复选框 - 见stackoverflow.com/questions/64573910/…
      【解决方案3】:

      好的,这需要一些认真的挖掘,但我想通了(如果我正确理解你的问题)。

      如果您想获取刚刚连接其 Google 日历的帐户的电子邮件地址,请使用以下代码。

      $this->service = new Google_Service_Calendar($this->client);
      $CalendarEmailAddress = $this->service->calendars->get('primary')->id;
      

      $this->client 是使用 $this->client = new Google_Client(); 创建的。

      您不再需要任何范围。我只使用以下范围:

      private $required_scopes=array(    
          Google_Service_Calendar::CALENDAR,
          Google_Service_Calendar::CALENDAR_EVENTS,
      );
      

      我在这里找到了 api 资源:https://developers.google.com/resources/api-libraries/documentation/calendar/v3/php/latest/class-Google_Service_Calendar_Calendars_Resource.html,尽管这是我见过的最糟糕的文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-09
        • 2014-01-19
        • 2012-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多