Php 插入事件时缺少结束时间Google日历
我正在尝试在Google日历中插入事件。身份验证和获取事件的操作没有任何问题。不幸的是,我收到了以下错误消息:Php 插入事件时缺少结束时间Google日历,php,http,post,google-calendar-api,Php,Http,Post,Google Calendar Api,我正在尝试在Google日历中插入事件。身份验证和获取事件的操作没有任何问题。不幸的是,我收到了以下错误消息: { error: { errors: [ { domain: "global", reason: "required", message: "Missing end time." } ], code: 400, message: "Missing en
{
error: {
errors: [
{
domain: "global",
reason: "required",
message: "Missing end time."
}
],
code: 400,
message: "Missing end time."
}
}
我请求在PHP中使用cURL的站点。要计算请求的内容长度,请使用函数countArrayChars。以下是我的代码:
$start = date("c", strtotime($start));
$end = date("c", strtotime($end));
$data_array = array(
"end"=>array("dateTime"=>$end),
"start"=>array("dateTime"=>$start),
"summary"=>$name
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://www.googleapis.com/calendar/v3/calendars/primary/events',
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => array("Content-length: ".countArrayChars($data_array),"Content-type: application/json","Authorization: Bearer $access_token\r\n"),
CURLOPT_POSTFIELDS => $data_array
));
$resp = curl_exec($curl);
curl_close($curl);
echo $resp;
function countArrayChars(array $array){
$charNumber = 0;
array_walk_recursive($array, function($val, $key) use (&$charNumber)
{
$charNumber += strlen($val) + strlen($key);
});
return $charNumber;
}
我还尝试将数据设置为http_build_查询以及json_编码结果。不幸的是,这也不起作用
谢谢你的回复
更新:
更新2:
这是$end的输出
我不知道这是否对您有帮助,但这是我用来通过API向google日历添加事件的代码:
$event = new Google_Service_Calendar_Event(array(
'summary' => $summary,
'location' => $location,
'description' => $description,
'start' => array(
'dateTime' => $startdatetime,
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => $enddatetime,
'timeZone' => 'America/Los_Angeles',
),
'reminders' => array(
'useDefault' => FALSE,
),
));
你通常需要像我一样添加时区,这可能是你的问题。我建议先试试
更新:
我找不到任何其他可能与您的代码错误的地方,这可能与谷歌如何接受数据有关。我现在能提供给你的就是我的工作方式,我知道这是可行的:
首先,如果尚未设置服务帐户,请按照说明进行操作。确保创建一个.p12文件,而不是JSON文件
您也可以下载PHP谷歌日历文件,而无需使用composer
接下来,使用下面的代码来满足您的需要,但这应该适合您。日期时间在邮寄之前已正确格式化,因此您可能需要更改它
header('Content-type: application/json');
require_once __DIR__ . '/google-api-php-client/src/Google/autoload.php';
$summary = $_POST["summary"];
$location = $_POST["location"];
$description = $_POST["description"];
$startdatetime = $_POST["startdatetime"];
$enddatetime = $_POST["enddatetime"];
$client_email = "clientemail"; //client email setup when you create the authorization in Google Developer Console.
$private_key = file_get_contents("privatekey.p12"); //location on your server where the .p12 file you created is stored
$scopes = array('https://www.googleapis.com/auth/calendar');
$user_to_impersonate = "primary"; //This can also be an email address associated with the current gmail login if you don't want to use the default one
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
$user_to_impersonate
); //Keep everything in this array the same
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$event = new Google_Service_Calendar_Event(array(
'summary' => $summary,
'location' => $location,
'description' => $description,
'start' => array(
'dateTime' => $startdatetime,
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => $enddatetime,
'timeZone' => 'America/Los_Angeles',
),
'reminders' => array(
'useDefault' => FALSE,
),
));
$service = new Google_Service_Calendar($client);
$calendarId = $useremail;
$event = $service->events->insert($calendarId, $event);
echo json_encode($event);
检查$start是否小于$end。谷歌的错误不正确。
您必须检查输入字段中是否有奇怪的符号。我在locationfield中找到了Straße…这给出了缺少的结束时间错误。谢谢您的回答。但不幸的是,这使用了用于PHP的GoogleAPI客户端库。我想试着通过卷曲来使用它。我添加了时区属性,但它也不起作用。你能更新你的问题以显示你是如何添加时区的吗?必须将它添加到结束时间所在的同一数组中,否则它将不起作用。只是出于好奇,您在哪里声明了原始的$end变量?我看到的只是$end=datec,strotime$end;但是没有原始的$end变量来执行strotime$end。它是来自POST还是GET请求?是的,它将通过GET请求获得它。好的,很好。您是否尝试将新的$end变量打印到屏幕上以确保格式正确?Try$end=datec,strottime$end;echo$end;
$event = new Google_Service_Calendar_Event(array(
'summary' => $summary,
'location' => $location,
'description' => $description,
'start' => array(
'dateTime' => $startdatetime,
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => $enddatetime,
'timeZone' => 'America/Los_Angeles',
),
'reminders' => array(
'useDefault' => FALSE,
),
));
header('Content-type: application/json');
require_once __DIR__ . '/google-api-php-client/src/Google/autoload.php';
$summary = $_POST["summary"];
$location = $_POST["location"];
$description = $_POST["description"];
$startdatetime = $_POST["startdatetime"];
$enddatetime = $_POST["enddatetime"];
$client_email = "clientemail"; //client email setup when you create the authorization in Google Developer Console.
$private_key = file_get_contents("privatekey.p12"); //location on your server where the .p12 file you created is stored
$scopes = array('https://www.googleapis.com/auth/calendar');
$user_to_impersonate = "primary"; //This can also be an email address associated with the current gmail login if you don't want to use the default one
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
$user_to_impersonate
); //Keep everything in this array the same
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$event = new Google_Service_Calendar_Event(array(
'summary' => $summary,
'location' => $location,
'description' => $description,
'start' => array(
'dateTime' => $startdatetime,
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => $enddatetime,
'timeZone' => 'America/Los_Angeles',
),
'reminders' => array(
'useDefault' => FALSE,
),
));
$service = new Google_Service_Calendar($client);
$calendarId = $useremail;
$event = $service->events->insert($calendarId, $event);
echo json_encode($event);