簡體   English   中英

Google 日歷 API - 獲取用戶的 email 地址以將其添加為與會者

[英]Google Calendar API - Get user's email address to add them as attendee

我有一個谷歌日歷 API 設置 Oauth 2.0。 我的網絡應用程序有老師和學生。 教師創建活動,學生可以加入。 我已經為所有用戶創建了將他們的 Google 帳戶與日歷 API 的權限相關聯的功能,並且教師可以創建事件並成功刪除它們,該事件被添加到他們的primary日歷中並從中刪除。

為此,我不需要老師的帳號email地址。 當他們授權我的應用程序 API 訪問他們的帳戶時,我會得到一個帶有CALENDAR_EVENTS scope 的令牌,我可以單獨使用它在他們的日歷中創建一個事件。 令牌內容是這樣的:

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

我也有學生的 Oauth 2 授權,帶有類似的令牌。 當學生加入活動時,我想更新活動並將該學生添加為與會者。 他們在我的應用程序上的帳戶 email 地址可能與他們為我的應用程序授權 Oauth 2 時使用的 Google 帳戶不匹配。 因此,我需要一種方法來獲取他們授權的帳戶的 Google email 地址。

這是將授權用戶添加為與會者的正確過程嗎? 我四處尋找獲取用戶帳戶信息的方法,發現大多是過時的信息。 我更願意使用來自 PHP Google SDK 的 Google_Client( Google_Client() object,或者更好的是Google_Service_Calendar() object。

如前所述,我只請求Google_Service_Calendar::CALENDAR_EVENTS scope。是否需要添加更多 scope 才能獲取此信息? 我還找到了這個Google Identify 文檔,但不確定我是否需要它。

我嘗試使用另一種方法來嘗試使用 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 

編輯:

閱讀第一個答案后,我閱讀了此處的文檔並使用了文檔中提供的 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 object。 但是,它沒有解釋從哪里獲得 $id_token。 從這篇文章中看到 id_token 是 Oauth2 響應中的一個字段,但我的響應 JSON 不包含該字段。

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

  • 使用此范圍,您將能夠檢索用戶的電子郵件。

要獲取您用戶的電子郵件地址,您可以在他們的用戶授權您的應用使用 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 獲取用戶的電子郵件。

好的,這需要一些認真的挖掘,但我想通了(如果我正確理解你的問題)。

如果你想獲取剛剛連接他們的谷歌日歷的帳戶的 email 地址,請使用以下代碼。

$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盡管這是我見過的最糟糕的文檔見過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM