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'); } } |
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 | <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/ mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> <script src="//code.jquery.com/mobile/1.3.2/ jquery.mobile-1.3.2.min.js"></script> </head> <body> <div data-role="page"> <div data-role="header"> Tasks </div> <div data-role="content"> <div data-role="collapsible-set" data-inset="false"> <?php foreach ($lists['items'] as $list): ?> <?php $id = $list['id']; ?> <div data-role="collapsible"> <h2><?php echo $list['title']; ?></h2> <ul data-role="listview"> <?php if (isset($tasks[$id]['items'])): ?> <?php foreach ($tasks[$id]['items'] as $task): ?> <li> <h3><?php echo $task['title']; ?></h3> </li> <?php endforeach; ?> <?php endif; ?> </ul> </div> <?php endforeach; ?> </div> </div> </div> </body> </html> |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |