-->

allow my user to add meeting in my calendar with G

2020-03-30 08:05发布

问题:

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

    ?>

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(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 in /Users/macbook/Desktop/testcal/google-api-php-client-2.2.2/src/Google/Http/REST.php on line 118

回答1:

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 "\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;
  }

}

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 "\ncalendar id: " . $calendarListEntry->getId();
    echo "\n------------------------------\n\n";