<?php
include('lead1.php');
require_once __DIR__ . '/vendor/autoload.php';
global $link;
$emailmsgsql = "SELECT *
FROM psleads WHERE agreeid = '6'";
$msgreqsres = mysqli_query($link, $emailmsgsql); // or die(mysql_error()0);
$msgreqs = $msgreqsres->fetch_assoc();
$start = $msgreqs['contractbegindate'] . ' ' . $msgreqs['contractbegintime'];
$end = $msgreqs['contractenddate'] . ' ' . $msgreqs['contractendtime'];
$startDT = new DateTime($start, new DateTimeZone('Pacific/Honolulu'));
$endDT = new DateTime($end, new DateTimeZone('Pacific/Honolulu'));
$startDTw3c = $startDT->format(DateTime::W3C);
$endDTw3c = $endDT->format(DateTime::W3C);
putenv('GOOGLE_APPLICATION_CREDENTIALS=./service-account.json');
define('CREDENTIALS_PATH', '~/calendar-php.json');
define('CLIENT_SECRET_PATH', './client_secret.json');
//define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
$client = new Google_Client();
$client->setApplicationName("Paradise_Sound_Booking_Calendar");
$client->addScope('https://www.googleapis.com/auth/calendar');
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setClientId('532085378494-s908fs5mu4rf2e2s60cecgaprg9pem1p.apps.googleusercontent.com');
$client->setDeveloperKey("XXXXX");//flo.gd
$client->useApplicationDefaultCredentials();
// Load previously authorized credentials from a file.
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
//$authCode = trim(fgets(STDIN));
$authCode = 'Manually pasted return code into script here';
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
'summary' => 'Booked Event ' . $msgreqs['contractbegindate'],
'start' => array(
'dateTime' => $startDTw3c,
//'dateTime' => '2015-05-28T09:00:00-07:00',
'timeZone' => 'Pacific/Honolulu',
),
'end' => array(
'dateTime' => $endDTw3c,
'timeZone' => 'Pacific/Honolulu',
)
));
$calendarId = 'iddnpsbinrifod2826eqo1kmoo@group.calendar.google.com';
$eventres = $service->events->insert($calendarId, $event);
echo json_encode($eventres);
?>
所以这是我用来测试事件插入我的谷歌日历的PHP代码 .
我以为我只能使用一个API密钥,但谷歌似乎有这种令人沮丧的做OAUTH的方式,我无法弄明白 . 我可以在API开发者控制台中看到所有403错误 .
有没有人有工作代码在我的日历中插入简单的事件 .
CONTEXT:
我将从paypal(已完成)接收IPN,这将触发此脚本,该事件将在我的日历中插入事件,而不是用户 . 任何人都可以帮助我,而不是指向我的谷歌开发人员文档?他们看起来很稀疏,我一遍又一遍地阅读它们,无法解决我的问题 .
这是我得到的错误:
致命错误:未捕获的异常'Google_Service_Exception',消息'{“错误”:{“errors”:[{“domain”:“global”,“reason”:“forbidden”,“message”:“Forbidden”}], “code”:403,“message”:“禁止”}}'在/home/dahfrench/flo.gd/src/Google/Http/REST.php:118堆栈跟踪:#0 /home/dahfrench/flo.gd /src/Google/Http/REST.php(94):Google_Http_REST :: decodeHttpResponse(对象(GuzzleHttp \ Psr7 \ Response),对象(GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#1 [内部函数] :Google_Http_REST :: doExecute(对象(GuzzleHttp \ Client),对象(GuzzleHttp \ Psr7 \ Request),'Google_Service _...')#2 /home/dahfrench/flo.gd/src/Google/Task/Runner.php( 181):call_user_func_array(Array,Array)#3 /home/dahfrench/flo.gd/src/Google/Http/REST.php(58):Google_Task_Runner-> run()#4 /home/dahfrench/flo.gd/ src / Google / Client.php(789):Google_Http_REST :: execute(Object(GuzzleHttp \ Client),Object(GuzzleHttp \ Psr7 \ Request),'Google_Service _...',Array)#5 / home / dahfrench / flo . GD / src目录/谷歌/服务/ Resource.ph p(232):第118行/home/dahfrench/flo.gd/src/Google/Http/REST.php中的Goo
1 回答
您可能希望最终确定要实施的身份验证:用户需要登录才能执行/请求Google服务或将域范围的权限委派给服务帐户 .
如果您将使用OAuth 2.0:
Sample Code from Google:
如果您将使用Google Apps域范围的授权:
来自SO post的示例代码:
Note :如果您打算只使用一个日历,我建议您使用服务帐户,然后将您的日历共享到该帐户,以避免403:Forbidden,如相关SO post所述
希望这可以帮助 .