Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. function getClient() {
  2. $client = new Google_Client();
  3. $client->setApplicationName("DL Calendar");
  4. $client->setAuthConfig('application/client_secrets.json');
  5. $client->addScope('profile');
  6. $client->setIncludeGrantedScopes(true);
  7. $client->setAccessType('offline');
  8.  
  9. return $client;
  10. }
  11.  
  12. function gcalendar() {
  13. $this->load->add_package_path(APPPATH . 'vendor/autoload');
  14.  
  15. $client = $this->getClient();
  16. //$client->setRedirectUri(site_url('calendar/index'));
  17. $client->addScope(Google_Service_Calendar::CALENDAR);
  18.  
  19. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  20.  
  21. $client->setAccessToken($_SESSION['access_token']);
  22. $access_token = $_SESSION['access_token'];
  23.  
  24. $service = new ]Google_Service_Calendar($client);
  25. $calendar = new Google_Service_Calendar_Calendar();
  26. //$calendarList = $service->calendarList->listCalendarList();
  27. $calendar = $service->calendars->get('primary');
  28.  
  29. $params = array(
  30. 'owner_id' => get_current_user_id(),
  31. 'title' => get_current_user(). ' ' .'Google Calendar',
  32. 'type' => 'gcal',
  33. 'url' => $calendar->id,
  34. );
  35.  
  36. $calendar_id = $this->Calendar_model->add_calendar($params);
  37. redirect('calendar/index');
  38.  
  39. } else {
  40.  
  41. $redirect_uri = site_url('calendar/oauth2callback');
  42. header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  43. }
  44. $this->session->set_flashdata('success', 'Event Successfully Added');
  45. }
  46.  
  47. //Build the client object
  48. $client = $this->getClient();
  49. $client->addScope(Google_Service_Calendar::CALENDAR);
  50. $service = new Google_Service_Calendar($client);
  51.  
  52. $url = parse_url($_SERVER['REQUEST_URI']); parse_str($url['query'], $params);
  53. $code = $params['code'];
  54.  
  55. //To exchange an authorization code for an access token, use the authenticate method:
  56. if (! isset($code)) {
  57. $auth_url = $client->createAuthUrl();
  58. header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
  59.  
  60. } else {
  61.  
  62. $token = $client->fetchAccessTokenWithAuthCode($code);
  63. $client->setAccessToken($token);
  64. $client->authenticate($code);
  65. $_SESSION['access_token'] = $client->getAccessToken();
  66. $redirect_uri = site_url('calendar/gcalendar');
  67. header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  68. }
  69.  
  70. function gcalendar_events() {
  71. $client = $this->getClient();
  72.  
  73. $client->addScope(Google_Service_Calendar::CALENDAR);
  74. // $client->setRedirectUri(site_url('calendar/gcalendar'));
  75. $client->setAccessType('offline'); //need calendar events to appear even if not logged in to google
  76. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  77.  
  78. $client->setAccessToken($_SESSION['access_token']);
  79. $access_token = $_SESSION['access_token'];
  80.  
  81. $service = new Google_Service_Calendar($client);
  82. $id = 'primary';
  83. $calendar = new Google_Service_Calendar_Calendar();
  84. $calendar = $service->calendars->get('primary');
  85.  
  86. $event = new Google_Service_Calendar_Event();
  87.  
  88. $events = $service->events->listEvents($id);
  89.  
  90. foreach ($events->getItems() as $event) {
  91.  
  92. $startTime = strtotime($event->getStart()->dateTime) ;
  93. $endTime = strtotime($event->getEnd()->dateTime);
  94. $start = date('Y-m-d H:i:s', $startTime);
  95. $end = date('Y-m-d H:i:s', $endTime);
  96.  
  97. $eventsArr[] = array(
  98. 'title' => $event->getSummary(),
  99. 'start'=> $start,
  100. 'end' => $end,
  101. );
  102. }
  103. // Return a single `events` with all the `$eventsArr`
  104. echo json_encode($eventsArr);
  105.  
  106.  
  107. }
  108. }
  109.  
  110. if ($client->isAccessTokenExpired()) {
  111. $refresh_token = $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
  112. $client->setAccessToken($refresh_token);
  113. $_SESSION['access_token'] = $refresh_token;
  114. $this->load->helper('file');
  115. write_file('application/client_secrets.json', json_encode($client->getAccessToken()));
  116. } else {
  117. $access_token = $_SESSION['access_token'];
  118. }
  119.  
  120. $token = $client->getAccessToken();
  121. $authObj = json_decode($token);
  122. if(isset($authObj->refresh_token)) {
  123. save_refresh_token($authObj->refresh_token);
  124. }
  125.  
  126. $client->refreshToken($your_saved_refresh_token);
  127.  
  128. $_SESSION['access_token'] = $client->getAccessToken();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement