-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.GoogleApi.php
More file actions
94 lines (80 loc) · 2.92 KB
/
Copy pathclass.GoogleApi.php
File metadata and controls
94 lines (80 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
//
// class.GoogleAPI.php - google api wrapper class
//
// Vers. ALPHA, YP 03/22/2016
//
//
// History:
// ALPHA, YP 03/22/2016 - Initial release.
//
require 'google-api-php-client/src/Google/autoload.php';
class GoogleAPI {
function __construct($cfg) {
$this->CFG = $cfg;
$client = $this->getGoogleApiClient(); // Get the API client
$this->Service = new Google_Service_Calendar($client); // Construct the service object.
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
private function getGoogleApiClient() {
$client = new Google_Client();
$client->setApplicationName($this->CFG['GOOGLE_API_APPLICATION_NAME']);
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfigFile($this->CFG['GOOGLE_API_CLIENT_SECRET_PATH']);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = $this->CFG['GOOGLE_API_CREDENTIALS_PATH'];
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} 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->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
//
// add_event - create event in google calendar
// Call: $id = add_event($start,$end, $msg)
// Call: $id = add_event($set)
// Where: $id - event Id if it was created successfully
// $set - - array of settings
// $set[start],$set[end] - start and end time of the event in the format YYYY-MM-DD HH:MM:SS
// $set[msg] - event subject
// $set[descr] - event description
//
public function add_event($set) {
$event = new Google_Service_Calendar_Event(array(
'summary' => $set['msg'],
'description' => array_key_exists('descr',$set) ? $set['descr'] : '',
'start' => array(
'dateTime' => date('Y-m-d\TH:i:s'.$this->CFG['GOOGLE_API_CALENDAR_TIMEZONE'], strtotime($set['start']))
),
'end' => array(
'dateTime' => date('Y-m-d\TH:i:s'.$this->CFG['GOOGLE_API_CALENDAR_TIMEZONE'], strtotime($set['end']))
),
'colorId' => array_key_exists('colorId',$set) ? $set['colorId'] : 0,
));
$result = $this->Service->events->insert($this->CFG['GOOGLE_API_CALENDAR_ID'], $event);
return $result->id;
}
}