PHP google apicalendar v3 auth 2.0 authentication error

Asked 2 years ago, Updated 2 years ago, 100 views

I would like to run Google API calendar quickstart.php to access the calendar with Auth 2.0, and eventually modify it to read and write events.

The google apic client contains the
I am using google-api-php-client-2.1.3 and

The auth authentication client ID is created as Other and downloaded as client_secret.json.

In the working directory, composer is also installed and we ran php quickstart.php in this state (https://developers.google.com/google-apps/calendar/quickstart/php).

The code executed is as follows:

<?php
require_once__DIR__.'/Google-api-php-client-2.1.3/src/Google/autoload.php';

define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
define('CLIENT_SECRET_PATH',__DIR__.'client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at~/.credentials/calendar-php-quickstart.json
define('SCOPES',implode(',array()
  Google_Service_Calendar::CALENDAR)
));

if(php_sapi_name()!='cli'){
  through new Exception('This application must be run on the command line.');
}

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
*/
function getClient(){
 $client=newGoogle_Client();
 $client->setApplicationName(APPLICATION_NAME);
 $client->setScopes (SCOPES);
 $client->setAuthConfig(CLIENT_SECRET_PATH);
 $client->setAccessType('offline');

 // Load previously authorized credentials from a file.
 $credentialsPath=expandHomeDirectory(CREDENTIALS_PATH);
 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);

 // 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;
}

/**
* Expands the home directory alias'~'to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
functionexpandHomeDirectory($path){
 $homeDirectory=getenv('HOME');
if(empty($homeDirectory)){
  $homeDirectory=getenv('HOMEDRIVE').getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}

// Get the API client and construct the service object.
$client=getClient();
$service=newGoogle_Service_Calendar($client);

// Print the next 10 events on the user's calendar.
$calendarId='[email protected]';
$optParams=array(
  'maxResults' = > 10,
  'orderBy' = > 'startTime',
  'singleEvents' = > TRUE,
  'timeMin' = > date('c'),
);
$results=$service->events->listEvents($calendarId,$optParams);

if(count($results->getItems())==0){
 print "No upcoming events found.\n";
} else{
 print "Upcoming events:\n";
 foreach($results->getItems() as $event){
  $start = $event->start->dateTime;
  if(empty($start)){
    $start = $event->start->date;
  }
  printf("%s(%s)\n", $event->getSummary(), $start);
 }
}

However, the following error occurred:

php quickstart.php
PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in/var/wwww/html/google-api-php-client-2.1.3/src/Google/Client.php: 849
Stack trace:
#0/var/www/html/quickstart.php(25):Google_Client-
>setAuthConfig('/var/www/html/n...')
#1/var/www/html/quickstart.php(73): getClient()
#2 {main}
crown in /var/www/html/google-api-php-client-2.1.3/src/Google/Client.php on line 849

I tried to resolve this error, but I couldn't find a solution.
Please let me know if there is anything you have overlooked.

Reference URL
https://github.com/google/google-api-php-client/issues/988
https://github.com/google/google-api-php-client/issues/1055

google-api

2022-09-30 18:36

1 Answers

This may be a late reply, but

define('CLIENT_SECRET_PATH',__DIR__.'client_secret.json');

I think it will come out if the above paths are different.

I got the same error. I rewritten the path file name of the json file to my environment.I get another error.

For your reference.


2022-09-30 18:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.