title: Integrate Google Calendar API into WordPress Plugin
body: <p>I've a calendar plugin working with my WordPress and I want to integrate Google Calendar API to Create, Read, Update, Delete Google Calendar Events. This is the process I followed.</p>
  
  <p>I've created a function inside different Calendar plugin Class, that I got from the Google Calendar API documentation (<a href="https://developers.google.com/calendar/quickstart/php" rel="nofollow noreferrer">https://developers.google.com/calendar/quickstart/php</a>):</p>
  
  <pre><code>function getClient() {
          try{
              $client = new Google_Client();
              $client-&gt;setApplicationName('Google Calendar API');
              $client-&gt;setScopes(Google_Service_Calendar::CALENDAR);
              $client-&gt;setAuthConfig(AEC_PATH .'credentials.json');
              $client-&gt;setAccessType('offline');
              $client-&gt;setPrompt('select_account consent');
  
              // Load previously authorized token from a file, if it exists.
              // The file token.json stores the user's access and refresh tokens, and is
              // created automatically when the authorization flow completes for the first
              // time.
              $tokenPath = 'token.json';
              if (file_exists($tokenPath)) {
                  $accessToken = json_decode(file_get_contents($tokenPath), true);
                  $client-&gt;setAccessToken($accessToken);
              } else {
                  print 'Access Token not generated!!';
              }
  
              // If there is no previous token or it's expired.
              if ($client-&gt;isAccessTokenExpired()) {
                  // Refresh the token if possible, else fetch a new one.
                  if ($client-&gt;getRefreshToken()) {
                      $client-&gt;fetchAccessTokenWithRefreshToken($client-&gt;getRefreshToken());
                  } else {
                      // Request authorization from the user.
                      $authUrl = $client-&gt;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-&gt;fetchAccessTokenWithAuthCode($authCode);
                      $client-&gt;setAccessToken($accessToken);
  
                      // Check to see if there was an error.
                      if (array_key_exists('error', $accessToken)) {
                          throw new Exception(join(', ', $accessToken));
                      }
                  }
                  // Save the token to a file.
                  if (!file_exists(dirname($tokenPath))) {
                      mkdir(dirname($tokenPath), 0700, true);
                  }
                  file_put_contents($tokenPath, json_encode($client-&gt;getAccessToken()));
              }
  
              return $client;
          } catch(Exception $e) {
              print_r($e-&gt;getMessage());
          }
  </code></pre>
  
  <p>Now I've added an action hook in constructor to initialize this function.</p>
  
  <pre><code>add_action('init', array($this, 'getClient'));
  </code></pre>
  
  <p>Now I'm calling this function getClient() in another function to call at particular time interval.</p>
  
  <pre><code>try{
              // Get the API client and construct the service object.
              $client = $this-&gt;getClient();
              $service = new Google_Service_Calendar($client);
              $calendarId = '&lt;Google_Calendar_ID&gt;';
  
              $event = new Google_Service_Calendar_Event(array(
                  'summary' =&gt; $input-&gt;title,
                  'location' =&gt; $input-&gt;address.''.$input-&gt;city.''.$input-&gt;state.''.$input-&gt;country.''.$input-&gt;zip,
                  'description' =&gt; $input-&gt;description,
                  'start' =&gt; array(
                      'dateTime' =&gt; '2019-09-22T09:00:00-07:00'
                  ),
                  'end' =&gt; array(
                      'dateTime' =&gt; '2019-09-22T17:00:00-07:00'
                  ),
                  'reminders' =&gt; array(
                      'useDefault' =&gt; FALSE,
                      'overrides' =&gt; array(
                      array('method' =&gt; 'email', 'minutes' =&gt; 24 * 60),
                      array('method' =&gt; 'popup', 'minutes' =&gt; 10),
                      ),
                  ),
              ));
  
              $event = $service-&gt;events-&gt;insert($calendarId, $event);
              echo "--Google Event Created--&gt;";
              print_r($event);
          } catch(Exception $e) {
              print_r($e-&gt;getMessage());
          }
  </code></pre>
  
  <p>Instead of new event created to my Google Calendar I'm getting an authorization error.</p>
  
  <pre><code>{
   "error": {
    "errors": [
     {
      "domain": "global",
      "reason": "insufficientPermissions",
      "message": "Insufficient Permission: Request had insufficient authentication scopes."
     }
    ],
    "code": 403,
    "message": "Insufficient Permission: Request had insufficient authentication scopes."
   }
  </code></pre>
  
  <p>Could you please help me in understanding why I'm getting this error and how can I resolve it ?</p>