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
| <?php
session_start();
require_once 'vendor/Slim/Slim.php';
require_once 'vendor/google-api-php-client/src/Google_Client.php';
require_once 'vendor/google-api-php-client/src/contrib/Google_TasksService.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->config(array(
'debug' => true,
'templates.path' => './templates'
));
$client = new Google_Client();
$client->setApplicationName('Project X');
$client->setClientId('YOUR-CLIENT-ID');
$client->setClientSecret('YOUR-CLIENT-SECRET');
$client->setRedirectUri('http://tasks.melonfire.com/login');
$client->setScopes(array(
'https://www.googleapis.com/auth/tasks'
));
$app->client = $client;
$app->tasksService = new Google_TasksService($app->client);
$app->get('/login', function () use ($app) {
if (isset($_GET['code'])) {
$app->client->authenticate();
$_SESSION['access_token'] = $app->client->getAccessToken();
$app->redirect('/index');
exit;
}
// if token available in session, set token in client
if (isset($_SESSION['access_token'])) {
$app->client->setAccessToken($_SESSION['access_token']);
}
if ($app->client->getAccessToken()) {
if (isset($_SESSION['target'])) {
$app->redirect($_SESSION['target']);
} else {
$app->redirect('/index');
}
} else {
$authUrl = $app->client->createAuthUrl();
$app->redirect($authUrl);
}
});
$app->get('/index', 'authenticate', function () use ($app) {
$lists = $app->tasksService->tasklists->listTasklists();
foreach ($lists['items'] as $list) {
$id = $list['id'];
$tasks[$id] = $app->tasksService->tasks->listTasks($id);
}
$app->render('index.php', array('lists' => $lists, 'tasks' => $tasks));
});
$app->get('/logout', function () use ($app) {
unset($_SESSION['access_token']);
$app->client->revokeToken();
});
$app->run();
function authenticate () {
$app = \Slim\Slim::getInstance();
$_SESSION['target'] = $app->request()->getPathInfo();
if (isset($_SESSION['access_token'])) {
$app->client->setAccessToken($_SESSION['access_token']);
}
if (!$app->client->getAccessToken()) {
$app->redirect('/login');
}
}
|