10

I'm using google slide API to connect to my project account, the connection worked perfectly, but sometime I get this error :

Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 503, "message": "The service is currently unavailable.", "errors": [ { "message": "The service is currently unavailable.", "domain": "global", "reason": "backendError" } ], "status": "UNAVAILABLE" } } in /vendor/google/apiclient/src/Google/Http/REST.php:118 Stack trace: #0 /vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 /vendor/google/apiclient/src/Google/Task/Runner.php(181): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run in /vendor/google/apiclient/src/Google/Http/REST.php on line 118

This is my getClient function Google API:

function getClient(string $SCOPES,string $CLIENT_SECRET_PATH,string $CREDENTIALS_PATH,string $APPLICATION_NAME) { $this->client->setApplicationName($APPLICATION_NAME); $this->client->setScopes($SCOPES); $this->client->setAuthConfig($CLIENT_SECRET_PATH); $this->client->setAccessType('offline'); $this->client->setApprovalPrompt('force'); $credentialsPath = $this->expandHomeDirectory($CREDENTIALS_PATH); if (file_exists($credentialsPath)) { $accessToken = json_decode(file_get_contents($credentialsPath), true); } else { $authUrl = $this->client->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); $accessToken = $this->client->fetchAccessTokenWithAuthCode($authCode); if(!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, json_encode($accessToken)); printf("Credentials saved to %s\n", $credentialsPath); } $this->client->setAccessToken($accessToken); if ($this->client->isAccessTokenExpired()) { $this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($this->client->getAccessToken())); } return $this->client; } 

This is the request the I send:

public function replaceAllShapesWithImage(array $data_images) { $requests =array(); if(isset($data_images)){ foreach ($data_images as $key => $value) { $requests[] = new \Google_Service_Slides_Request(array( 'replaceAllShapesWithImage' => array( 'imageUrl' => $value['url'], 'replaceMethod' => $value['replaceMethod'], 'containsText' => array( 'text' => $value['text'] ) ) )); } } return $requests; } 

$data_image has this value:

'replaceAllShapesWithImage' => array( 0 => array( 'text'=>'{{LOGO}}', 'url'=>'http://www.16cafe.com/wp-content/themes/16cafe/css/images/logo.png', 'replaceMethod'=>'CENTER_INSIDE' ), 1 => array( 'text'=>'{{PHOTO}}', 'url'=>'http://localhost:9880/wp-content/uploads/2017/02/pla23.jpg', 'replaceMethod'=>'CENTER_INSIDE' ), ) 
5
  • which line is throwing the error? Commented Mar 2, 2017 at 10:34
  • I update my question that you can see lines error Commented Mar 2, 2017 at 10:44
  • 2
    @MALKIMOHAMED can you provide the body of the request that is causing the 503? the code you pasted only does the oauth flow, and doesnt make any Slides API calls. Commented Mar 3, 2017 at 1:18
  • I agree with @MauriceCodik please provide a stackoverflow.com/help/mcve Commented Mar 3, 2017 at 7:41
  • ok @MauriceCodik I update my question again to add the request Commented Mar 3, 2017 at 8:44

3 Answers 3

5

Every time I catch an API error my script waits 1 second. If the error occurs again the script waits 2 seconds. Then 4 seconds, 8 seconds, 16 seconds. In total I make 5 attempts. It works every time.

public function get_analytics_response() { $api_call_num_of_attempts = 5; $api_call_attempts = 0; $api_call_sleep = 1; do { try { $response = $this->analytics->reports->batchGet(); } catch (Google_Service_Exception $e) { // Sleep 1s, 2s, 4s, 8s, 16s + random milliseconds $sleep = ($api_call_sleep * 1000000) + rand(1000, 1000000); usleep($sleep); $api_call_attempts++; $api_call_sleep = $api_call_sleep * 2; $error_message_json = $e->getMessage(); $error_message = json_decode($error_message_json, true); // Save errors... continue; } break; } while ($api_call_attempts < $api_call_num_of_attempts); return $response; } 
Sign up to request clarification or add additional context in comments.

Comments

2

Slides can definitely give better error messaging, but I'm pretty sure the problem is that http://localhost:9880/wp-content/uploads/2017/02/pla23.jpg image URL you are trying to insert. I tried making a replaceAllShapesWithImage request with that image and also got a 503.

Slides downloads your images over the public internet, so a localhost domain wont work. Try hosting it on some publicly accessible URL, or you can use Google Drive to host the image.

Comments

2

I had the same error message (for a totally different reason).

For me, I waited 30 seconds and attempted the same thing again, and it worked.

Seems it may simply be a brief outage of the Google API.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.