I'm using the Google PHP API Client v2.0 in an app I'm running on Google App Engine on localhost. When I publish my app to Google App Engine production, I haven't gotten the error, but it's intermittent so it's hard to know if I'm just getting lucky in production.
My Google API client is attempting to write a file to my Google Drive. This code has been working well for a couple of months, but it suddenly stopped working this morning. I checked the Google API status and they aren't reporting any outages. When I execute any of the API calls, I get this vague error response:
Fatal error: fopen(): Invalid response from API server. in /Users/me/googleappengine/stuff-otherstuff-111111/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php on line 312
The file does get created on Google Drive, so my API call is getting through, but whatever response is coming back to my script is causing a fatal crash.
For a few minutes it started working again, now it is crashing again. I can't really find any solutions to the error Invalid response from API server anywhere... and it is intermittent. It doesn't appear to be something to do with my code, but Google says it's not having any kind of outage.
What am I missing? How can I fix this?
<?php
include_once('code-base.php');
require_once('vendor/autoload.php');
$client = new Google_Client();
$client->setApplicationName(getSetting('APP_NAME'));
$client->setAuthConfig(json_decode(getSetting('localhost_google_client_secret'), true));
$client->setAccessType("offline");
$client->setScopes(array(Google_Service_Drive::DRIVE));
$client->setHttpClient(new GuzzleHttp\Client(['verify'=>'ca-bundle.crt']));
$accesstoken = json_decode(getSetting('localhost_google_oauth_token'), true);
$client->setAccessToken($accesstoken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
print "access token is expired\n";
$refreshtoken = getSetting('localhost_google_refresh_token');
print "refresh token: $refreshtoken\n";
$client->refreshToken($refreshtoken);
$newtokenjson = json_encode($client->getAccessToken());
print "new token: $newtokenjson\n";
printf("before: %s\n\nafter: %s\n\n", $accessToken, $newtokenjson);
//file_put_contents($credentialsPath, $newtokenjson);
updateSetting('localhost_google_oauth_token', $newtokenjson);
}
print "after checking access token expired\n";
print "before drive service\n";
$driveservice = new Google_Service_Drive($client);
print "after drive service\n";
$parentfolderid = getSetting('GDRIVE_EXPORT_DUMP');
$title = "00005 test gdrive.csv";
$filetype = 'text/csv';
$contents = "The quick brown fox jumped over the lazy dog.";
$file = new Google_Service_Drive_DriveFile();
$file->setName($title);
$file->setDescription($title);
$file->setMimeType($filetype);
$file->setParents(array($parentfolderid));
try {
if ($contents == null) {
print "contents of file are null, creating empty file\n";
$createdFile = $driveservice->files->create($file);
print "after creating empty file\n";
} else {
$contentsarray = array('data' => $contents, 'mimeType' => $filetype, 'uploadType' => 'media');
if ($options != null) {
foreach($options as $key => $value) {
$contentsarray[$key] = $value;
}
}
print "file contents: ".var_export($contentsarray, true)."\n";
$createdFile = $driveservice->files->create($file, $contentsarray);
print "after create file with contents\n";
}
return $createdFile;
} catch (Exception $e) {
print "EXCEPTION: An error occurred: " . $e->getMessage()."\n";
}
EDIT: My code seems to be failing consistently now with the Invalid response from API server message. It only fails at a point where the code communicates with Google. Those two places are when I call refreshToken() (which only happens rarely) and the other is when I call create().
I had my friend run this exact code on his machine with the same setup (as far as we can tell... same code, same libs, same oauth token, etc.) and it works for him.
Notes: The getSetting() function the above code just pulls some strings from my database that I use for configuration purposes. Also, the setHttpClient() call is needed because this is running inside the Google App Engine which runs on PHP 5.5 which has a screwed up CA Bundle and it requires me to provide a proper one.
What could be causing it to fail for me every time but work for my friend?
fopenerror in theStreamHandlercode, I suspect the error occurs with either creating or reading a file from Drive. Please provide a minimal code sample that can reproduce this error and we'd be happy to look into it. - Nicholas