google-api-php-client icon indicating copy to clipboard operation
google-api-php-client copied to clipboard

Uncaught exception 'Google_Cache_Exception' with message 'Could not create storage directory

Open connexas opened this issue 11 years ago • 15 comments

I'm getting the following error while trying the example on https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-php with the latest code:

Warning: mkdir(): Permission denied in /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Cache/File.php on line 149 Fatal error: Uncaught exception 'Google_Cache_Exception' with message 'Could not create storage directory: /var/tmp//Google_Client/d7' in /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Cache/File.php:154 Stack trace: #0 /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Cache/File.php(139): Google_Cache_File->getCacheDir('306ab5989d4665e...', true) #1 /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Cache/File.php(134): Google_Cache_File->getCacheFile('306ab5989d4665e...', true) #2 /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Cache/File.php(95): Google_Cache_File->getWriteableCacheFile('306ab5989d4665e...') #3 /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Auth/OAuth2.php(315): Google_Cache_File->set('306ab5989d4665e...', '{"access_token"...') #4 /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/HelloAnalytics.php(31): Google_Auth_O in /mnt/web1/e0/53/44455566/htdocs/myApp123/admin/google-api-php-client-master/src/Google/Cache/File.php on line 154 

After investigation I found the issue in file "google-api-php-client-master/src/Google/Cache/File.php" in function "getCacheDir($file, $forWrite)". The storage directory is created like:

$storageDir = $this->path . '/' . substr(md5($file), 0, 2);

But the error in my case is the concatenated slash after the path. It already contains a slash! Refer to error message:

/var/tmp//Google_Client/d7'

After the "tmp" folder there are 2 slashes now that raises the error. If I remove the concatenation of this slash, it works fine.

connexas avatar Jun 02 '15 10:06 connexas

Is that path being generated automatically, or have you set it in the config?

ianbarber avatar Jun 03 '15 03:06 ianbarber

I didn't set the path by myself. I'm even not allowed to do changes for that. I only can use it because the scripts are running at a big webhosting company.

connexas avatar Jun 03 '15 06:06 connexas

Yeah, I think we could probably be defensive here and check for a trailing slash before adding.

ianbarber avatar Jun 03 '15 20:06 ianbarber

I get the same error message if I run code using this library with two different user accounts. The /tmp/google-api-php-client folder is owned by the first user who runs the code, and can't be accessed by other users.

alcoheca avatar Nov 11 '15 11:11 alcoheca

this is strange, I would expect the permissions to be set by the web server.

If you're executing a script, just specify a cache directory with proper permissions (i.e. an an existing directory with a group permission that includes both users).

bshaffer avatar Nov 11 '15 20:11 bshaffer

I'm running it in a CLI manner.

Also I wasn't aware you could specify the cache directory, need to RTFM a bit closer

alcoheca avatar Nov 16 '15 19:11 alcoheca

I'm not sure it's in the manual. But yeah you can do this:

$client = new Google_Client();
$client->setCache(new Google_Cache_File('/path/to/shared/cache'));

bshaffer avatar Nov 16 '15 19:11 bshaffer

Amazing, thanks!

alcoheca avatar Nov 16 '15 19:11 alcoheca

Hello, I get a similar error whenI Insert Google Calendar Entries with Service Account: `Warning: mkdir(): Permission denied in /mydomain.com/app/google-api-php-client/src/Google/Cache/File.php on line 149

Fatal error: Uncaught exception 'Google_Cache_Exception' with message 'Could not create storage directory: /tmp/Google_Client/d5' in/mydomain.com/app/google-api-php-client/src/Google/Cache/File.php:154 Stack trace: #0 /mydomain.com/app/google-api-php-client/src/Google/Cache/File.php(139): Google_Cache_File->getCacheDir('5277a54f911ee9a...', true) #1 /mydomain.com/app/google-api-php-client/src/Google/Cache/File.php(134): Google_Cache_File->getCacheFile('5277a54f911ee9a...', true) #2 /mydomain.com/app/google-api-php-client/src/Google/Cache/File.php(95): Google_Cache_File->getWriteableCacheFile('5277a54f911ee9a...') #3 /mydomain.com/app/google-api-php-client/src/Google/Auth/OAuth2.php(322): Google_Cache_File->set('5277a54f911ee9a...', '{"access_token"...') #4 /mydomain.com/app/_database/db_tools.php(1361): Google_Auth_OAuth2->refreshTokenWithAssertion(Object(Google_Auth_AssertionCredentia in /mydomain.com/app/google-api-php-client/src/Google/Cache/File.php on line 154`

My code works perfectly in localhost but when I run it in my server It shows this error above.

betollaque avatar Mar 08 '16 20:03 betollaque

@betollaque what version of the library are you using? I imagine this is a permissions issue, and the web server does not have permissions to write to the temp directory. Try fixing the permissions or setting the cache to something else:

$client->setCache(new Google_Cache_Memcache);

bshaffer avatar Mar 15 '16 21:03 bshaffer

Hello! I found a solution: Config.php Line 143: 'directory' => '/path/to/shared/cache' This works for me. I'm using the last stable realese 1.1.7.

betollaque avatar Mar 15 '16 21:03 betollaque

Glad to hear it!

bshaffer avatar Mar 15 '16 22:03 bshaffer

$client = new Google_Client();
$client->setClassConfig('Google_Cache_File', array('directory' => '/path/to/cache/directory'));

incloud-ar avatar May 26 '16 22:05 incloud-ar

I'm seeing this problem. It seems to make the login fail.

gbittmann avatar Jun 17 '16 19:06 gbittmann

// Here is how I fixed it in my own code for my Google calendar service when I moved to a different host without the need to touch Google's code.

$config = new Google_Config(); $config->setClassConfig('Google_Cache_File', array('directory' => '../tmp/cache')); $client = new Google_Client($config);

awl100 avatar Jun 25 '16 09:06 awl100