【问题标题】:The service is currently unavailable Google api该服务目前不可用 Google api
【发布时间】:2017-07-22 00:45:25
【问题描述】:

我正在使用 google slide API 连接到我的项目帐户,连接工作正常,但有时我收到此错误:

致命错误:未捕获 Google_Service_Exception: { "error": { "code": 503, "message": "服务当前不可用。", "errors": [ { "message": "服务当前不可用.", "domain": "global", "reason": "backendError" } ], "status": "UNAVAILABLE" } } 在 /vendor/google/apiclient/src/Google/Http/REST.php:118 堆栈中跟踪:#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->在第 118 行的 /vendor/google/apiclient/src/Google/Http/REST.php 中运行

这是我的 getClient 函数 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;
}

这是我发送的请求:

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 有这个值:

'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'
            ),
        )

【问题讨论】:

  • 哪一行报错了?
  • 我更新了我的问题,你可以看到行错误
  • @MALKIMOHAMED 您能否提供导致 503 的请求正文?您粘贴的代码仅执行 oauth 流程,不进行任何 Slides API 调用。
  • 我同意@MauriceCodik 请提供stackoverflow.com/help/mcve
  • ok @MauriceCodik 我再次更新我的问题以添加请求

标签: php google-api access-token google-slides-api


【解决方案1】:

每次我发现 API 错误时,我的脚本都会等待 1 秒。如果错误再次发生,脚本将等待 2 秒。然后是 4 秒、8 秒、16 秒。我总共做了 5 次尝试。每次都有效。

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;
}

【讨论】:

    【解决方案2】:

    幻灯片肯定可以提供更好的错误消息,但我很确定问题在于您尝试插入的 http://localhost:9880/wp-content/uploads/2017/02/pla23.jpg 图像 URL。我尝试使用该图像发出replaceAllShapesWithImage 请求,也得到了 503。

    Slides 通过公共 Internet 下载您的图像,因此 localhost 域将不起作用。尝试将其托管在某个可公开访问的 URL 上,或者您可以使用 Google Drive 托管图像。

    【讨论】:

      【解决方案3】:

      我收到了同样的错误信息(出于完全不同的原因)。

      对我来说,我等了 30 秒,然后再次尝试同样的事情,结果成功了。

      看来这可能只是 Google API 的短暂中断。

      【讨论】:

        猜你喜欢
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-30
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 2015-08-17
        相关资源
        最近更新 更多