Google-api-php-client: Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist'

Created on 16 Sep 2016  路  12Comments  路  Source: googleapis/google-api-php-client

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'file does not exist' in 
/var/www/mysite/googleapi2/vendor/google/apiclient/src/Google/Client.php:839 Stack trace: 
#0 /var/www/mysite/googleapi2/quick.php(32): Google_Client->setAuthConfig('/var/www/mysite...') 
#1 /var/www/mysite/googleapi2/quick.php(80): getClient() 
#2 {main} thrown in /var/www/mysite/googleapi2/vendor/google/apiclient/src/Google/Client.php on line 839

Here is my code and also fyi

define('CREDENTIALS_PATH', '/<myjson>.json'); 
define('CLIENT_SECRET_PATH', **DIR** . '/<myclient>.googleusercontent.com.json');

i have put ./.json or ../.json on credentials_path is still not working

What i'm i going wrong? This is the latest version on the composer.I regenerate again but its still occuring please advise

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once **DIR** . '/vendor/autoload.php';

define('APPLICATION_NAME', 'Sample');
define('CREDENTIALS_PATH', '/<myjson>.json'); 
define('CLIENT_SECRET_PATH', **DIR** . '/<myclient>.googleusercontent.com.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Drive::DRIVE_METADATA_READONLY)
));
echo php_sapi_name()."sdf";

/* if (php_sapi_name() != 'cli') {
  throw 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 = new Google_Client();
  $client->setApplicationName(APPLICATION_NAME);
  $client->setScopes(SCOPES);
  $client->setAuthConfig(CLIENT_SECRET_PATH);
  $client->setAccessType('offline');
    $client->useApplicationDefaultCredentials();
  // 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.
  */
  function expandHomeDirectory($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 = new Google_Service_Drive($client);

// Print the names and IDs for up to 10 files.
$optParams = array(
  'pageSize' => 10,
  'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);

if (count($results->getFiles()) == 0) {
  print "No files found.\n";
} else {
  print "Files:\n";
  foreach ($results->getFiles() as $file) {
    printf("%s (%s)\n", $file->getName(), $file->getId());
  }
}

Most helpful comment

@developerside @globe194 This exception is being thrown because the path supplied to setAuthConfig does not exist. See the code here for reference.

Whatever path you've defined as CLIENT_SECRET_PATH is either not the correct path to your credentials file, or the file is unreadable (i.e. the web server does not have permissions to open the file). Here's what you can do to fix this:

  1. Verify the path to your file is what you think it is:

    var_dump(CLIENT_SECRET_PATH);
    // this will output your file path before the exception is thrown
    
  2. Verify the file exists

    $ cat /path/to/yourfile.json
    
  3. Verify the file is readable

    $ chmod a+r /path/to/yourfile.json
    

All 12 comments

Did you find a solution?

Hi @awoyele sorry but no one seems to care

I'm having the same issue :/ Any solutions yet?

Did you follow the Instructions in Step 1. on this site?
https://developers.google.com/google-apps/calendar/quickstart/php

yes i have followed all the steps but its not working

@developerside @globe194 This exception is being thrown because the path supplied to setAuthConfig does not exist. See the code here for reference.

Whatever path you've defined as CLIENT_SECRET_PATH is either not the correct path to your credentials file, or the file is unreadable (i.e. the web server does not have permissions to open the file). Here's what you can do to fix this:

  1. Verify the path to your file is what you think it is:

    var_dump(CLIENT_SECRET_PATH);
    // this will output your file path before the exception is thrown
    
  2. Verify the file exists

    $ cat /path/to/yourfile.json
    
  3. Verify the file is readable

    $ chmod a+r /path/to/yourfile.json
    

I had this error until reading this and figuring out that path to my auth file was wrong.

I am facing same issue.. i revised my first step. i forgot to add clinet_secret.json. After adding this file, my error is solved.

Sorry, but I have the file in the right directory and still get this error.

client_secrets.json with an s, I missed the s

Awesome! @bshaffer two years later and your answer is still helping people ;)

Step 1: Enable the Analytics API
To get started using Google Analytics API, you need to first use the setup tool, which guides you     through creating a project in the Google API Console, enabling the API, and creating credentials.
Create a client ID

Open the Service accounts page. If prompted, select a project.
Click Create service account.
In the Create service account window, type a name for the service account, and select Furnish a new     private key. If you want to grant G Suite domain-wide authority to the service account, also select Enable     G Suite Domain-wide Delegation. Then click Save.

Your new public/private key pair is generated and downloaded to your machine; it serves as the only     copy of this key. You are responsible for storing it securely.
When prompted for the Key type select JSON, and save the generated key as service-account-    credentials.json; you will need it later in the tutorial.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexandreGerault picture AlexandreGerault  路  5Comments

mevsme picture mevsme  路  4Comments

Fredyy90 picture Fredyy90  路  3Comments

usamamashkoor picture usamamashkoor  路  5Comments

whatido1 picture whatido1  路  3Comments