Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wfsmith's avatar

artisan config:cache breaks app. It can't find credentials file referenced in .env

I have an environment variable for Google APIs credentials:

GOOGLE_APPLICATION_CREDENTIALS="/var/www/application_root/credentials/google/credentials.json"

In config/app.php, I have this:

'google_app_creds' => ENV('GOOGLE_APPLICATION_CREDENTIALS'),

Everything works great until I run php artisan config:cache. Then the application suddenly can't access the credentials json file and I get this error:

production.ERROR: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information

After I run php artisan config:clear, the error goes away and my google api content loads perfectly.

I checked in bootstrap/cache/config.php and the environment variable looks fine. Any ideas?

0 likes
12 replies
Snapey's avatar

where do you use this config setting?

1 like
wfsmith's avatar

Hi Snapey. Its in a custom ServiceProvider in app/Packages.

Snapey's avatar

@wfsmith as in, show the code......

So you are putting a string into config which you then load and parse as a filepath?

Test:

Open tinker and type config('app.google_app_creds')

1 like
wfsmith's avatar

@Snapey yes that is correct. Google APIs generates a json file for authentication via a service account using their PHP SDK.

And I am storing the json in a folder on the root of the application called /credentials which is .gitignored. For deployment, you generate the json, place it in that folder on the server after composer install and everything runs fine until you run php artisan config:cache

Snapey's avatar

Search your codebase for any reference to "env(" that is not in the config folder.

1 like
wfsmith's avatar

Do you mean any reference to env( that are not in the .env file? Or .env variables that I have not added to app/config.php?

I can only find one .env variable that is being referenced directly in the code without using the config('app.var_name') syntax. It an environment check for a artisan command in app/Console/Kernal.php .

I thought there would be more but that's the only reference to a .env variable without it being somewhere in config/*.php and using the config('app.var_name') syntax.

There are a number of vestigial .env variables that i've left in there but do not use and are not referenced anywhere else. E.g. VITE_PUSHER_APP_KEY and a bunch of others from .env.example

Thank you for your patience with me!

click's avatar

I can say off the bat that there are definitely a few .env variables that are being reference directly in the code with out using the config('app.var_name') syntax.

env('YOUR_VAR) will be empty if you cache your config file. Make sure you only use env('YOUR_VAR') in your config files. And use in all other locations config('file.your_var')

More information can be found in the documentation: https://laravel.com/docs/10.x/configuration#configuration-caching

After you fixed above issue, what is the value of config('app.google_app_creds') after you cached the config? Is it really empty?

1 like
wfsmith's avatar

@click Ah interesting. i updated my post since you reply. I was wrong. There is only one .env variable not being referenced using the config( syntax, and its for an artisan command.

click's avatar

@wfsmith what does "Then the application suddenly can't access the credentials json file and I get this error:" mean. Is it a console application, a queued job, a webpage?

And what is the value of config('app.google_app_creds') after you cached the config? Is it really empty if you debug it?

1 like
wfsmith's avatar

@click Its a website. An API specifically. I can explain further but essentially you can make a request to a /stories endpoint and if you include ?mostviewed=true, it will use Google API to fetch the top N stories from Google Analytics and pass back the stories that are the most viewed in the last 48 hours.

When I cache the config file, this endpoint /stories?mostviewed is a 500 and returns the error I mention in the original post. When I clear the config, the request is a 200 and it correctly returns the stories.

The .env variable in question is in config/app.php:

'google_app_creds' => ENV('GOOGLE_APPLICATION_CREDENTIALS'),

And its only referenced once in the code in a custom ServiceProvider package where I initialize the Google client.

EDIT

Sorry, forgot your last question. Yes, the variable is correct in bootstrap/cache/config.php after running config:cache:

    'google_app_creds' => '/var/www/api.example.com/credentials/google/apis.json',
    'google_profile_id' => '1234567890',
click's avatar

@wfsmith I never worked with the google API but if you go to the link they suggest you to visit (https://cloud.google.com/docs/authentication/application-default-credentials#GAC) it says

You can use the GOOGLE_APPLICATION_CREDENTIALS environment variable to provide the location of a credential JSON file.

This will not work with your environment variable and config caching as all the env() responses will be null, see laravel's documentation mentioned above.

If you google on the combination laravel GOOGLE_APPLICATION_CREDENTIALS you will find more of the same questions and with solutions.

https://stackoverflow.com/questions/46397023/loading-google-application-credentials-in-laravel-5 shows a suggestion:

$storage = new StorageClient([
   'keyFilePath' => config('app. google_app_creds'),
]);
1 like
wfsmith's avatar

@click Thank you. While this solution didn't work, it set me on the right path.

it looks like this authentication approach cannot read environment variables at all even if using the Google\Cloud\StorageClient package:

$client->useApplicationDefaultCredentials();

But authenticating with $client->setAuthConfig(PATH_TO_JSON) is able to read the environment variable:

$client->setAuthConfig(config('app.google_app_creds'));

Here is the solution that worked for me:

function initializeGA()
{
  $client = new \Google\Client();
  try {
    $client->setAuthConfig(config('app.google_app_creds'));
    $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
    return $client;
  }
  catch(\Exception $e) {
    return null;
  }
}

Seems so simple in retrospect. I don't know why the Google SDK doesn't work with both approaches. Dare I say its a bug in the SDK?

Thank you @click and @snapey for your help in solving this!

Please or to participate in this conversation.