我正在尝试使用PHP和V3 api为Google日历设置推送通知。
我拥有Auth2.0权限,可以从我的应用程序在谷歌上创建事件。现在我想知道用户何时对google日历进行任何更改(CRUD操作)。
这是我的代码:
private $imageService;
public $google_client;
public $google_calendar;
public function __construct()
{
$this->imageService = new ImageService();
$this->google_client = new Google_Client();
$this->google_client->setApplicationName($_ENV['GOOGLE_APP_NAME']);
$this->google_client->setDeveloperKey($_ENV['GOOGLE_API_KEY']);
$this->google_client->setClientId($_ENV['CLIENT_ID']);
$this->google_client->setClientSecret($_ENV['CLIENT_SECRET']);
$this->google_client->setAccessType('offline');
$this->google_client->setIncludeGrantedScopes(true);
$this->google_client->setScopes(array('email', 'profile', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/calendar'));
$this->google_calendar = new Google_Service_Calendar($this->google_client);
}
public function googleCalendarWatch($uuid){
$channel = new Google_Service_Calendar_Channel($this->google_client);
$channel->setId($uuid);
$channel->setType('web_hook');
$channel->setAddress("https://example.com/google/googleNotifications");
$channel->setExpiration("1919995862000");
$this->google_calendar->events->watch('primary', $channel);
}
这是输出:
Google_Service_Calendar_Channel Object (
[internal_gapi_mappings:protected] => Array ( )
[address] =>
[expiration] => 1426272395000
[id] => aee2b430-34bf-42bc-a597-ada46db42799
[kind] => api#channel
[params] =>
[payload] =>
[resourceId] => 51IKGpOwCJ6EMraQMUc1_04MODk
[resourceUri] => https://www.googleapis.com/calendar/v3/calendars/primary/events?key=AIzaSyBFUvq3OZO6ugAKvz7l8NgLS0V6DUJq8Vc&alt=json
[token] =>
[type] =>
[modelData:protected] => Array ( )
[processed:protected] => Array ( ) )
到目前为止,我不知道为什么address返回null,也许这就是问题所在。但我不知道如何修复它。
同时阅读以下内容:#26730263和我自己的代码没有太大区别。
我做了google说的所有事情,注册域、添加凭据、api密钥、允许推送域等等。。
<?php
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} 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));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();
if (empty($events)) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($events as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
printf("%s (%s)\n", $event->getSummary(), $start);
}
}
完整教程
为每个资源创建一个通知通道,然后将通知您对该资源的任何修改。以下信息直接来自谷歌(创建通知渠道)。
提出手表请求:
每个可观察的Google日历API资源在以下形式的URI上都有一个关联的监视方法:
https://www.googleapis.com/**apiName**/**apiVersion**/**resourcePath**/watch
要为有关特定资源更改的消息设置通知通道,请向资源的监视方法发送POST请求。每个通知通道都与特定用户和特定资源(或一组资源)相关联。除非当前用户拥有或拥有访问此资源的权限,否则监视请求将不会成功。
例子:
开始监视给定日历上事件集合的更改:
POST https://www.googleapis.com/calendar/v3/calendars/my_calendar@gmail.com/events/watch
Authorization: Bearer auth_token_for_current_user
Content-Type: application/json
{
"id": "01234567-89ab-cdef-0123456789ab", // Your channel ID.
"type": "web_hook",
"address": "https://example.com/notifications", // Your receiving URL.
...
"token": "target=myApp-myCalendarChannelDest", // (Optional) Your channel token.
"expiration": 1426325213000 // (Optional) Your requested channel expiration time.
}
}
参考:谷歌(创建通知渠道)(2018年3月19日)。
我最近为谷歌日历配置了谷歌推送通知。我使用推送通知通知在关注的日历中创建/更新/删除的事件,它工作得非常好,我使用双向同步。 但是,我在此设置中发现了一个小故障。我有一个创建事件的本地应用程序。它与Google日历同步(使用API调用),Google再次将事件通知给我的本地应用程序。这会导致事件重复。 如何区分从以下2种情况收到的推送通知? 案例1:用户在Google日历中创建或其他一些应用程序创
尊敬的谷歌日历团队:, 我们想为日历开发推送通知。我已经看完了这篇文章https://developers.google.com/google-apps/calendar/v3/push. 但我有几个问题:- 我们也有C语言的日历消息推送吗? 如何获取通知通道id? 域在这种情况下是什么意思?域将始终gmail.com。 你能分享一个可以显示日历工作消息推送的示例吗? -拉胡尔
我正在使用API explorer和Chrome Advanced Rest Client根据给定的示例观看事件资源。https://developers.google.com/google-apps/calendar/v3/push#watch_request_examples 要求 回应 > 我在google上搜索了这个问题,但找不到太多帮助。 有人能告诉我,请求有什么问题吗?
我正在使用php watch commant: 这个命令工作正常,我得到的响应是: 然而在我的设置url中,当日历中发生更改时,我不会收到任何消息。我错过什么了吗!?
从昨天早上开始,我一直在处理google drive API请求。 正如这里所解释的:https://developers.google.com/drive/api/v3/push 我正在尝试订阅发送此请求的通知: Url:https://www.googleapis.com/drive/v3/changes/watch 响应为代码400,主体为: 此订阅请求不需要此参数,但此订阅请求需要此参数(
我认为通过使用,我可以捕获请求正文,但事实是它什么也没有捕获。如果我理解正确,我应该收到一个结构在这里详细说明的变更资源。是我做错了什么,还是我理解错了?我很感激任何可以给予的洞察力和提前感谢!