0 レビュー
1 回答
サービスアカウントカレンダーではなく、デフォルトカレンダーでのPHPによるGoogleカレンダーイベントの作成
Googleカレンダーにイベントを追加するために、クライアントID、サービスアカウント名、p12キーを取得するGoogleサービスアカウントを作成しました。私のコードからイベントを作成していますが、サービスアカウントの名前でイベントを挿入するための独自の新しいカレンダーを作成しました。 Googleアカウントのデフォルトのカレンダーにイベントを追加したい。以下は、サービスアカウントイベント挿入の作業コードです。
function calendarize ($title, $desc, $start_datetime, $end_datetime, $gmail_id, $location)
{
$start_ev_datetime = new DateTime($start_datetime, new DateTimeZone('America/Chicago'));
$start_ev_datetime = $start_ev_datetime->format('c');
$end_ev_datetime = new DateTime($end_datetime, new DateTimeZone('America/Chicago'));
$end_ev_datetime = $end_ev_datetime->format('c');
//Google credentials
$client_id = '**********.apps.googleusercontent.com';
$service_account_name = '*******.iam.gserviceaccount.com';
$key_file_location = '**************.p12';
if (!strlen($service_account_name) || !strlen($key_file_location))
echo missingServiceAccountDetailsWarning();
$client = new Google_Client();
$client->setApplicationName("App Name");
if (isset($_SESSION['token']) && $_SESSION['token']) {
$client->setAccessToken($_SESSION['token']);
if ($client->isAccessTokenExpired()) {
$access_token= json_decode($_SESSION['token']);
$client->refreshToken($access_token->refresh_token);
unset($_SESSION['token']);
$_SESSION['token'] = $client->getAccessToken();
}
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar'),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
$_SESSION['service_token'] = $client->getAccessToken();
$calendarService = new Google_Service_Calendar($client);
$calendarList = $calendarService->calendarList;
//Set the Event data
$event = new Google_Service_Calendar_Event();
$event->setSummary($title);
$event->setDescription($desc);
$event->setLocation($location);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($start_ev_datetime);
$start->setTimeZone('America/Chicago');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($end_ev_datetime);
$end->setTimeZone('America/Chicago');
$event->setEnd($end);
try {
$createdEvent = $calendarService->events->insert('primary', $event);
} catch (Exception $e) {
var_dump($e->getMessage());
}
$acl = $calendarService->acl->listAcl('primary');
$userExistFlag = false;
foreach ($acl->getItems() as $rule) {
if($rule->getId() == 'user:'.$gmail_id){
$userExistFlag = true;
}
}
if(!$userExistFlag){
$scope = new Google_Service_Calendar_AclRuleScope();
$scope->setType('user');
$scope->setValue( $gmail_id );
$rule = new Google_Service_Calendar_AclRule();
$rule->setRole( 'owner' );
$rule->setScope( $scope );
$result = $calendarService->acl->insert('primary', $rule);
}
echo 'Event Successfully Added with ID: '.$createdEvent->getId();
}
わからない
0
レビュー
答え :
解決策:
サービスアカウントはあなたではないことを忘れないでください。サービスアカウントは、それ自体のダミーユーザーです。独自のGoogleカレンダーアカウントを持っているため、自分のカレンダーではなくカレンダーにイベントを作成します。
サービスアカウントの電子メールアドレスを取得し、Webアプリケーションで他のユーザーと共有するのと同じように、個人のカレンダーをサービスアカウントと共有します。カレンダーをサービスアカウントと共有すると、カレンダーにアクセスできるようになります。カレンダーIDに注意してください。これを使用して、カレンダーを更新できます。
背景情報
$createdEvent = $calendarService->events->insert('primary', $event);
primaryは、現在認証されているユーザーのプライマリカレンダーを寄付します。それがサービスアカウントのプライマリカレンダーです。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。