Google PHP API: "incorrect response from api server"

I am using the Google PHP API Client v2.0 in an application that I run in the Google App Engine on localhost. When I publish my application for building Google App Engine applications, I didn’t get an error, but it’s intermittent, so it’s hard to understand if I will just be successful in production.

My Google API client is trying to write a file to its Google Drive. This code has been working well for a couple of months, but it stopped working this morning. I checked the status of the Google API and they do not report a disconnect. When I make any of the API calls, I get this undefined error response:

Fatal error : fopen (): Invalid API server response. at <Users/me/googleappengine/stuff-otherstuff-111111/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.phpon line 312

The file is created on Google Drive, so my API call passes, but any response returns to my script, causing a fatal failure.

Within a few minutes it started working again, now it is crumbling again. I cannot find any solutions for the Invalid response from API server error anywhere ... and this is intermittent. This does not look like my code, but Google says it does not have any shutdowns.

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 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 doesn't seem to work with the Invalid response from API server message now. This only happens where the code interacts with Google. These are two places when I call refreshToken() (which is rare) and the other is when I call create() .

I had a friend of mine who ran this exact code on her machine with the same setup (as far as we can tell ... the same code, the same files, the same oauth token, etc.) and it works for him.

Notes. The getSetting() function of the above code simply extracts some rows from my database that I use for configuration purposes. In addition, the call to setHttpClient() necessary because it runs inside the Google App Engine, which runs on PHP 5.5, which has an inflated CA Bundle, and it requires me to provide the correct one.

What can make him fail for me every time, but work for my friend?

+5
source share
1 answer

Before you can answer “what causes this error”, you first need to answer “what is the error”. As a rule, short-term network errors are normal and do not cause the application to crash with a fatal error, even if they occur constantly (for example, in the event of a failure or, in your case, a local problem).

Guzzle, of course, does not treat errors as fatal, as it sets its default HTTP context ignore_errors => true (and returns the answer anyway). google-api-php-client does not change the default behavior of ignore_errors , not google-api-php-client-services , neither the development server, nor the PHP SDK. The text Invalid response from API server not displayed anywhere in the source code for any of the above projects and is not a registered error message from any of the APIs.

Therefore, given that the source of the Invalid response from API server is somewhere inside the application code itself. I assume that the application code has a similar exception handling unit for all exceptions, since you are above, which swallows any response without an answer, and produces an opaque error message as fatal.

The best answer I can give is to find the source of the error message in your code, and then tell him to write down the details and not hide them, which can show what is the main problem and how to fix it.

-1
source

Source: https://habr.com/ru/post/1261288/


All Articles