簡體   English   中英

允許我的用戶使用帶有 PHP 的 Google Calendar API 在我的日歷中添加會議,無需身份驗證

[英]Allow my user to add meeting in my calendar with Google Calendar API with PHP without auth

我正在開發一個應用程序,允許我的用戶使用我的 Google 日歷之一安排/重新安排日歷事件,我不需要用戶向 Google 驗證自己的身份。 我需要使用的是帶有服務帳戶的 Google Calendar API。

我試圖用 api v2 寫一些東西,但我遇到了問題,所以我想知道你是否可以指導我閱讀教程或示例,或者只是插入的示例

<?php

require_once './google-api-php-client-2.2.2/vendor/autoload.php';


session_start();
/************************************************   */

$client_id = '751416246659ae431430560098c25ba433c3e483';
$Email_address = ' testecalendar@spartan-grail-241202.iam.gserviceaccount.com';
$key_file_location = './credentials.json';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);

// separate additional scopes with a comma   
$scopes = "https://www.googleapis.com/auth/calendar.readonly";
$client->setAuthConfig('./credentials.json');


$service = new Google_Service_Calendar($client);

?>

<html>

<body>

    <?php
    $calendarList  = $service->calendarList->listCalendarList();

    while (true) {
        foreach ($calendarList->getItems() as $calendarListEntry) {

            echo $calendarListEntry->getSummary() . "<br>\n";


            // get events 
            $events = $service->events->listEvents($calendarListEntry->id);


            foreach ($events->getItems() as $event) {
                echo "-----" . $event->getSummary() . "<br>";
            }
        }
        $pageToken = $calendarList->getNextPageToken();
        if ($pageToken) {
            $optParams = array('pageToken' => $pageToken);
            $calendarList = $service->calendarList->listCalendarList($optParams);
        } else {
            break;
        }
    }

    ?>

我收到此錯誤:

致命錯誤:未捕獲的 Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header" , "location": "Authorization" } ], "code": 401, "message": "Login Required" } } 在 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src /Google/Http/REST.php:118 堆棧跟蹤:#0 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(94): Google_Http_REST ::decodeHttpResponse(Object(GuzzleHttp\\Psr7\\Response), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...') #1 /Users/macbook/Desktop/testcal/google-api-php-client-2.2. 2/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\\Client), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...') #2 /Users/macbook/ Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(58): Google_Task_Runner->run() #3 /Users/macbook/Desktop/testcal/google-api- php-client-2.2.2/src/Google/Client.php(798): Google_Http_RES在第 118 行的 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php

在沒有用戶同意屏幕的情況下從 PHP 應用程序訪問 Google Calendar API


不確定你是否真的想經歷這一切(它只有一次 :-) ),但你去吧......


“服務帳戶是一種特殊類型的 Google 帳戶,旨在代表需要進行身份驗證並被授權訪問 Google API 中的數據的非人類用戶。”

這個想法是使用“服務帳戶”(電子郵件地址)授權/共享您的日歷,並使用“服務帳戶密鑰”進行身份驗證。


我將從代碼開始:


<?php
// read google calendar from php command line application

require_once './google-api-php-client-2.2.3/vendor/autoload.php';

$client = new Google_Client();
$client->addScope("https://www.googleapis.com/auth/calendar.readonly");

$client->setAuthConfig('client_credentials.json');
$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();

while(true) {

  foreach ($calendarList->getItems() as $calendarListEntry) {
    echo $calendarListEntry->getSummary();
    echo "\n------------------------------\n\n";

    // get events 
    $events = $service->events->listEvents($calendarListEntry->id);

    foreach ($events->getItems() as $event) {
        echo "- " . $event->getSummary() . "\n";
        echo "- " . $event->getStart()->getDateTime() . "\n\n";
    }

  }

  $pageToken = $calendarList->getNextPageToken();

  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $calendarList = $service->calendarList->listCalendarList($optParams);
  } else {
    break;
  }

}

如何使它工作:

從這個倉庫( https://github.com/googleapis/google-api-php-client/releases
下載“google-api-php-client-2.2.3.zip”並將“google-api-php-client-2.2.3”解壓縮到您的php應用程序的根目錄中。

接下來,您需要這個“client_credentials.json”文件。 您將從“Google Cloud Platform”獲得它。 最好是創建一個新項目,您可以使用現有項目,但您可能無法獲得我在下面描述的內容(您可能能夠做到,本質是相同的)。


創建並選擇一個新項目后,
- 轉到“API 和服務”>“庫”
- 搜索“日歷”,然后選擇“Google Calendar API”
- 使能夠

接下來,您需要“創建憑據”
- 如果您尚未重定向,請轉到“API 和服務”>“憑據”
- 點擊“創建憑據”>“幫我選擇”
- 他們會問你“你在使用哪個 API”,選擇“Google Calendar API”
- 接下來,他們會問你“你將從哪里調用 API?”,選擇“其他 UI(例如 Windows、CLI 工具)”
- 接下來,他們會問您“您將訪問哪些數據?”,選擇“應用程序數據”
- 接下來,單擊“我需要什么憑據?”

在此處輸入圖片說明


現在您位於“向項目添加憑據”頁面
- 填寫“服務帳戶名稱”
- 選擇“項目”的“角色”>“編輯器”
- “密鑰類型”應該是“JSON”
- 單擊“繼續”,系統會提示您下載此“something-something.json”文件(您可以將其保存在 php 應用程序的根目錄中)

在此處輸入圖片說明


接下來,在您的 php 應用程序文件夾中,將“something-something.json”重命名為“client_credentials.json”

差不多好了...

接下來,使用文本編輯器打開“client_credentials.json”
- 復制作為“client_email”值的電子郵件地址,例如“test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com”

接下來,轉到您的日歷(您的 Google 日歷,您要訪問的日歷)並轉到“設置”>“日歷設置”
- 向下滾動到“與特定的人分享”(您會在列表中看到自己)
- 單擊“+ 添加人員”並粘貼電子郵件地址,例如“test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com”
- 選擇您想要的“權限”
- 點擊“發送”按鈕

現在您的 php 應用程序通過服務帳戶可以訪問您的日歷。


這是顯示日歷 ID 的代碼:

    echo "calendar summary: " . $calendarListEntry->getSummary();
    echo "\ncalendar id: " . $calendarListEntry->getId();
    echo "\n------------------------------\n\n";

如果您嘗試使用服務帳戶,答案就在這里:DalmTo 詳細解釋。

谷歌日歷 API - PHP

我已經嘗試了所有其他示例,但沒有任何效果。

暫無
暫無

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

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