使用服务帐户在PHP上使用Google Calendar API [英] Google Calendar API on php using service account

查看:166
本文介绍了使用服务帐户在PHP上使用Google Calendar API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Google Calendar API 有疑问。



我使用PHP获取Google Calendar API,并使用服务帐户创建新事件。



如果我在php上阅读日历 progrum 再一次,只有使用服务帐户从php写入的日程才能加载。



原因是什么问题是什么?



它是一个规范,php无法读取从php创建的服务帐户计划吗?



设置信息:


  1. 我使用google-api-php-client-1-master的autoload.php。

  2. 在Google日历V3中。邮件帐户(服务帐户)被赋予日程安排更改权限,并且可以写入日历。
  3. 所有者在Google上直接创建的日程安排日历可以重新广告由php progrum制作,但由服务帐户通过php创建的日程安排无法加载。


我的代码

  //阅读活动

$ 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);



//创建活动

$ 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'=>'由服务帐户创建',$ b $'start'=>数组(
'dateTime'=> ;'2017-04-04T00:12:00 + 09:00',
'timeZone'=>'Asia / Tokyo',
),
'end'=>数组(
'dateTime'=>'2017-04-04T00:13:00 + 09:00',
'timeZone'=>'亚洲/东京',
),
));
$ event = $ service-> events-> insert($ calendarId,$ event);

但是,事件的创建者是由服务帐户的地址创建的,而不是所有者的邮件帐户。



你能告诉我这个答案吗?

解决方案

您需要记住,服务帐户不是您。将服务帐户视为虚拟用户。它有自己的Google日历帐户,自己的驱动器帐户,可能还有更多。将服务帐户中的日历插入到服务帐户所拥有的日历中。
$ b


但是,活动的创建者是由服务帐户的地址创建的,而不是所有者的邮件帐户。


活动的创建者是服务帐户应该是其创建活动的电子邮件帐户。

如果您希望特定用户添加事件,那么您应该使用Oauth2。

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?

解决方案

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.

这篇关于使用服务帐户在PHP上使用Google Calendar API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆