0

I am Integrating Google Calendar Api with PHP using PHP Client Library but when I am running the app through Command then It shows the Invalid Token Format but My ClientID and Client Secret is correct. My code is same as given in Google Calendar Api Docs but not working for me. If Any one can help me to sort out this problem.Just Stuck in that and unable to go further.

<?php
   require __DIR__ . '/vendor/autoload.php';
   if (php_sapi_name() != 'cli') {
     throw new Exception('This application must be run on the command line.');
   }
function getClient(){
   $client = new Google_Client();
   $client->setApplicationName('Google Calendar API PHP Quickstart');
   $client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
   $client->setAuthConfig('client_secret.json');
   $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = 'client_secret.json';
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->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->fetchAccessTokenWithAuthCode($authCode);

    // Check to see if there was an error.
    if (array_key_exists('error', $accessToken)) {
        throw new Exception(join(', ', $accessToken));
    }

    // Store the credentials to disk.
    if (!file_exists(dirname($credentialsPath))) {
        mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s\n", $credentialsPath);
  }
   $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
     $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
     file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  }
   return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Calendar($client);
// Print the next 10 events on the user's calendar.
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();
if (empty($events)) {
print "No upcoming events found.\n";
} else {
print "Upcoming events:\n";
foreach ($events as $event) {
    $start = $event->start->dateTime;
    if (empty($start)) {
        $start = $event->start->date;
    }
    printf("%s (%s)\n", $event->getSummary(), $start);
}
}
Sikandar_ali
  • 149
  • 1
  • 10
  • try a `var_dump($accessToken)` after this line `$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);`, you may have an error message captured in there – Dale Aug 13 '18 at 11:00
  • @Dale Now Error is `Uncaught InvalidArgumentException: missing the required redirect URI` But I am working on Localhost – Sikandar_ali Aug 13 '18 at 11:53

0 Answers0