79

I'm trying to setup a Push Notifications for Google Calendar using PHP and V3 api.

I've got the Auth2.0 Permission and I'm able to create events on google from my application. Now I want to know when a user makes any change on google calendar (CRUD Operations).

This is my code:

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);

}

And this is the output:

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 ( ) )

This far I don't know why address returns null, maybe that's the problem. But I don't know how to fix it.

Also reading this: #26730263 and looking my own code there's no much difference.

I did all the stuff that google says, Register domain, add credentials, api key, push domain allowed and all..

galaxy
  • 53
  • 1
  • 1
  • 15
  • Where is the part where you redirect the user to the google server to get the auth token? – Mathieu de Lorimier Apr 13 '16 at 16:50
  • @mathieu- that all are wrkg... im getting hits in my webhook url but its just blank...how can I pick the response from webhook. – Sinto Apr 16 '16 at 17:37
  • Your user needs to be authenticated for you to be able to add a new webhook – Mathieu de Lorimier Apr 16 '16 at 17:58
  • @MathieudeLorimier- I have done authentication & have a verified webhook. I'm getting something as above shown, but my issue is that I'm not getting any data(from webhook) when I'm updating a data in google calendar. – Sinto Apr 18 '16 at 06:41
  • 1
    @Sinto According to the first answer (in the questions linked to above) you don't get any data in the webhook, you only get notified that something changed. You then need to find out what changed. Or are you saying that you the hook doesn't get called at all? – Robbie Apr 19 '16 at 04:14
  • Robbie & MathieudeLorimier - Thank you, for the support. My feature got hold.. – Sinto Apr 19 '16 at 08:24
  • Don't give up before the goal :) Can you clarify what is the exact content what you get on webhook url? I've implemented a full sync solution couple of weeks ago, I can help you solve your problem. If you get a sync token in the push notification, you need only get a full list of events WITH the sync token as a parameter, then you will get the changed event(s) only in the result. – Peter Apr 18 '17 at 12:35
  • Just refer below link..I think it will help you,, [Click Here](https://stackoverflow.com/questions/19648611/setup-push-notifications-for-google-calendar-api-using-php-client) – Nikunj Oct 10 '17 at 12:30

2 Answers2

1

Create a notification channel for every individual resource, then any modification to that resource you will be then notified. Information below is directly from Google (Creating notification channels).

Making watch requests:

Each watchable Google Calendar API resource has an associated watch method at a URI of the following form:

https://www.googleapis.com/**apiName**/**apiVersion**/**resourcePath**/watch

To set up a notification channel for messages about changes to a particular resource, send a POST request to the watch method for the resource. Each notification channel is associated both with a particular user and a particular resource (or set of resources). A watch request will not be successful unless the current user owns or has permission to access this resource.

Example:

Start watching for changes to a collection of events on a given calendar:

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.
  }
}

Reference: Google (Creating notification channels) (19-Mar-2018).

  • Address?
  • Token?
BH7
  • 204
  • 2
  • 8
1
<?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);
    }
}

Full Tutorial

King Tyga
  • 31
  • 6