我正在尝试将日历事件放入日历中,而无需身份验证提示。我读到,使用 OAuth 服务帐户可以让您执行此操作。我已经在我的 Google 开发者控制台中设置了服务帐户,但对此没有任何运气。
我们有一个 Google 企业帐户,我将我的委托用户设置为我们帐户的管理员,以便他们可以访问任何人的日历,但不会创建活动。我认为问题出在访问令牌上。当我var_dump($accessToken)
表明NULL
.
<?php
error_reporting(E_ALL);
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
session_start();
/* Authentication ID's */
const CLIENT_ID = '[MY CLIENT ID]';
const SERVICE_ACCOUNT_NAME = '[MY SERVICE ACCOUNT]';
const KEY_FILE = '[MY KEY FILE].p12';
const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar";
$key = file_get_contents(KEY_FILE);
$auth = new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array(CALENDAR_SCOPE),
$key
);
$auth->sub = "[email protected]";
$client = new Google_Client();
$client->setScopes(array(CALENDAR_SCOPE));
$client->setAssertionCredentials($auth);
$client->getAuth()->refreshTokenWithAssertion();
$accessToken = $client->getAccessToken();
$client->setClientId(CLIENT_ID);
$user = "[email protected]";
if($accessToken){
$cal = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('TITLE');
$event->setLocation('World');
$event->setDescription('test');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDate(date('yyyy-mm-dd'));
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDate(date('yyyy-mm-dd'));
$event->setEnd($end);
$cal->events->insert($user, $event);
}
?>
我已经引用了另一个线程(使用服务帐户,getAccessToken() 返回 null)尝试解决此问题,但没有成功。
任何帮助将不胜感激!
听起来您需要授予对 google apps 域用户数据的访问权限。下面的链接位于“将域范围内的权限委托给您的服务帐户”部分。
https://developers.google.com/drive/web/delegation
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)