Google Calendar API on php using service account

不羁的心 提交于 2019-12-13 08:42:53

问题


I have a question about the Google Calendar API.

I get the Google Calendar API with PHP and created the new event with service account.

If I read the calendar on php progrum again, only the schedule written from php with service account can not be load.

What is the cause of the problem?

is it a specification that php can not read in a schedule created from php with a service account?

Setting information:

  1. I am using autoload.php of google-api-php-client-1-master.

  2. It is Google calendar V3.

  3. In the calendar ID, the shared e-mail account (service account) is given the "schedule change authority" and can be written to the calendar.

  4. The schedule that the owner directly created on Google Calendar can be read by php progrum, but the schedule created by php with the service account can not be load.

My code

//read event

$json_path = '../json/XXXX-123465789.json';
$json_string = file_get_contents($json_path, true);
$json = json_decode($json_string, true);
$private_key = $json['private_key'];
$client_email = $json['client_email'];
$scopes = array(Google_Service_Calendar::CALENDAR);
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP API");
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);


$read_start = mktime(0, 0, 0, 4, 10, 2017);
$read_end = mktime(0, 0, 0, 4, 17, 2017);

$calendarId = '123456789@group.calendar.google.com';
$optParams = array(
    'maxResults' => 99,
    'orderBy' => 'startTime',
    'singleEvents' => TRUE,
    'timeMin' => date('c', $read_start_date),
    'timeMax' => date('c', $read_end_date),
);

$results = $service->events->listEvents($calendarId, $optParams);



//create event

$json_path = './json/xxx-123456789.json';
$json_string = file_get_contents($json_path, true);
$json = json_decode($json_string, true);

$calendarId = '123456789@group.calendar.google.com';
$private_key = $json['private_key'];
$client_email = $json['client_email'];
$scopes = array(Google_Service_Calendar::CALENDAR);
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setApplicationName("Google Calendar");
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);


$meeting_end_time = _get_sum_time($set_time,$meeting_time);
$event = new Google_Service_Calendar_Event(array(
    'summary' => 'created by service account',
    'start' => array(
        'dateTime' => '2017-04-04T00:12:00+09:00',
        'timeZone' => 'Asia/Tokyo',
    ),
    'end' => array(
        'dateTime' => '2017-04-04T00:13:00+09:00',
        'timeZone' => 'Asia/Tokyo',
    ),
));
$event = $service->events->insert($calendarId, $event);

However, the creator of the event is created by address of the service account, not the owner's mail account.

Could you tell me this answer?


回答1:


You need to remember that a service account is not you. Think of a serice account as a dummy user. It has its own Google calendar account, its own drive account and probably a bunch more. When you insert a to a calendar on a service account you are inserting into the calendar owned by the service account.

However, the creator of the event is created by address of the service account, not the owner's mail account.

The creator of the event is the service account is should be its email account creating the event.

If you want a specific user to be adding events then you should be using Oauth2.



来源:https://stackoverflow.com/questions/43451846/google-calendar-api-on-php-using-service-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!