-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_event.php
More file actions
131 lines (115 loc) 路 4.36 KB
/
Copy pathcreate_event.php
File metadata and controls
131 lines (115 loc) 路 4.36 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
session_start();
include_once('resources.php');
$refresh = true;
if (isset($_SESSION['currentDate'])) {
$refresh = false;
$testDate = (new DateTime())->getTimestamp();
if ($testDate - $_SESSION['currentDate'] >= $_SESSION['expiresIn'])
$refresh = true;
}
// If the user doesn't have a token or if it expired we create another one
if ($refresh) {
$url = 'https://www.googleapis.com/oauth2/v4/token';
$data = array(
'code' => $_GET['code'],
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'redirect_uri' => URI,
'grant_type' => 'authorization_code'
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$json = json_decode(file_get_contents($url, false, $context));
if ($json === FALSE) {
echo 'Error, token expired; please log again';
return;
}
$_SESSION['accessToken'] = $json->access_token;
$_SESSION['currentDate'] = (new DateTime())->getTimestamp();
$_SESSION['expiresIn'] = $json->expires_in;
$_SESSION['refreshToken'] = $json->refresh_token;
}
$urlCalendar = 'https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token=' . $_SESSION['accessToken'];
$dataToSend = [
'start' => ['date' => '2017-03-09'],
'end' => ['date' => '2017-03-31'],
'summary' => 'A random event'
];
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($dataToSend)
)
);
$context = stream_context_create($options);
file_get_contents($urlCalendar, false, $context);
$events = json_decode(file_get_contents($urlCalendar))->items;
//header('Content-Type: application/json');
//var_dump($events);
?>
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>OAuth2 Example</title>
<link href="https://blackrockdigital.github.io/startbootstrap-bare/css/bootstrap.min.css" rel="stylesheet">
<style>
body { padding-top: 70px; }
</style>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">OAuth2 Example</a>
</div>
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<button class="btn btn-success"> Your event has been created ! </button> <br>
<h3>My events :</h3>
<ul class="list-group">
<?php
foreach($events as $event) {
echo '<li class="list-group-item"><b>' . $event->summary . '</b>';
// Some events do not have a period
if (property_exists($event,'start')){
if (property_exists($event->start,'date'))
echo '<span class="badge">' . $event->start->date . ' - ' . $event->end->date . '</span>';
}
echo '</li><hr>';
}
?>
</ul>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container -->
<!-- jQuery Version 1.11.1 -->
<script src="https://blackrockdigital.github.io/startbootstrap-bare/js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="https://blackrockdigital.github.io/startbootstrap-bare/js/bootstrap.min.js"></script>
</body></html>