允许我的用户使用带有 PHP 的 Google Calendar API 在我的日历中添加会议,无需身份验证

我正在开发一款应用,它允许我的用户使用我的一个 Google 日历安排/重新安排日历活动,我不需要用户在其中向 Google 进行身份验证.我需要使用的是带有服务帐户的 Google Calendar API.

我尝试用 api v2 写一些东西,但我遇到了问题,所以我想知道您是否可以将我引向教程或示例,或者只是插入示例

<?phprequire_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);//用逗号分隔附加作用域$scopes = "https://www.googleapis.com/auth/calendar.readonly";$client->setAuthConfig('./credentials.json');$service = new Google_Service_Calendar($client);?><身体><?php$calendarList = $service->calendarList->listCalendarList();而(真){foreach ($calendarList->getItems() as $calendarListEntry) {echo $calendarListEntry->getSummary() .<br>
";//获取事件$events = $service->events->listEvents($calendarListEntry->id);foreach ($events->getItems() as $event) {回声-----".$event->getSummary() .<br>";}}$pageToken = $calendarList->getNextPageToken();如果($pageToken){$optParams = array('pageToken' => $pageToken);$calendarList = $service->calendarList->listCalendarList($optParams);} 别的 {休息;}}?>

我收到此错误:

<块引用>

致命错误:未捕获的 Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": ";需要登录",locationType":header",location":Authorization"}],代码":401,消息":需要登录";在/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(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), '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(GuzzleHttpClient)), Object(GuzzleHttpPsr7Request), '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 in/Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php 第 118 行

解决方案

无需用户同意屏幕即可从 PHP 应用程序访问 Google Calendar API


不确定你是否真的想经历这一切(只有一次 :-) ),但是你去...


服务帐户是一种特殊类型的 Google 帐户,旨在代表需要进行身份验证并被授权访问 Google API 中的数据的非人类用户."

这个想法是使用服务帐户"(电子邮件地址)授权/共享您的日历,并使用服务帐户密钥"进行身份验证.


我将从代码开始:

<?php//从 php 命令行应用程序读取谷歌日历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();而(真){foreach ($calendarList->getItems() as $calendarListEntry) {echo $calendarListEntry->getSummary();echo "
------------------------------

";//获取事件$events = $service->events->listEvents($calendarListEntry->id);foreach ($events->getItems() as $event) {回声-".$event->getSummary() ."
";回声-".$event->getStart()->getDateTime() ."

";}}$pageToken = $calendarList->getNextPageToken();如果($pageToken){$optParams = array('pageToken' => $pageToken);$calendarList = $service->calendarList->listCalendarList($optParams);} 别的 {休息;}}

如何使它工作:

来自这个 repo (


现在您位于向项目添加凭据"页面
- 填写服务帐户名称"
- 选择项目"的角色">编辑器"
-密钥类型"应该是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();回声
日历ID:".$calendarListEntry->getId();echo "
------------------------------

";

I am working on a app that allows my users to schedule/reschedule Calendar events using one of my Google calendars, where I don’t need the users to authenticate themselves with Google. What i need to use is the Google Calendar API with a service account.

I tried to write something with api v2 but I had a problem, so I would like to know if you could direct me to a tutorial or an example, or just an example of insertion

<?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>
";


            // 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;
        }
    }

    ?>

I got this error:

Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } in /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php:118 Stack trace: #0 /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), '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(GuzzleHttpClient), Object(GuzzleHttpPsr7Request), '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 in /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php on line 118

解决方案

Access Google Calendar API from a PHP application without user consent screen


Not sure if you really want to go through all this ( it's only once :-) ), but here your go...


"A service account is a special type of Google account intended to represent a non-human user that needs to authenticate and be authorized to access data in Google APIs."

The idea is to authorize/share your calendar with a "Service account" (an email address) and authenticate with a "Service account key".


I'll start with the code :


<?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 "
------------------------------

";

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

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

";
    }

  }

  $pageToken = $calendarList->getNextPageToken();

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

}

How to make it work:

From this repo (https://github.com/googleapis/google-api-php-client/releases)
download "google-api-php-client-2.2.3.zip" and unzip "google-api-php-client-2.2.3" in the root of your php application.

Next, you need this "client_credentials.json" file. You'll get it from "Google Cloud Platform". Best would be to create a new project, you could use an existing project but you might not get what I describe below (you might be able to make it, the essence is the same).


After you create and select a new project,
- go to "APIs & Services" > "Library"
- search for "Calendar" then select "Google Calendar API"
- ENABLE

Next, you need to "CREATE CREDENTIALS"
- if you're not already redirected go to "APIs & Services" > "Credentials"
- click on "Create credentials" > "Help me choose"
- they'll ask you "Which API are you using", choose "Google Calendar API"
- next, they'll ask you "Where will you be calling the API from?", choose "Other UI (e.g. Windows, CLI tool)"
- next, they'll ask you "What data will you be accessing?", pick "Application data"
- next, click on "What credentials do I need?"


Now you're on "Add credentials to your project" page
- fill "Service account name"
- select "Role" of "Project" > "Editor"
- "Key type" should be "JSON"
- click "Continue" and you'll be prompted to download this "something-something.json" file (you can save it in the root of your php application)


Next, in your php application folder, rename the "something-something.json" to "client_credentials.json"

Almost there...

Next, open "client_credentials.json" with a text editor
- copy the email address that is the value of "client_email" something like "test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com"

Next, go to your calendar (your Google Calendar, the one you want to access) and go to "Settings" > "Calendar settings"
- scroll down to "Share with specific people" (you'll see yourself in the list)
- click on "+ Add people" and paste the email address the one like "test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com"
- select what you'd like for "Permissions"
- click on "Send" button

Now your php application via the service account has access to your calendar.


Here is the code to show the calendar id:

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

";

相关文章