该服务目前无法使用Google API

时间:2017-03-02 10:32:22

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

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

  

致命错误:未捕获Google_Service_Exception:{"错误":{"代码":503,"消息":"该服务目前无法使用。 ","错误":[{"消息":"该服务目前无法使用。","域":& #34; global"," reason":" backendError" }," status":"无法使用"在/vendor/google/apiclient/src/Google/Http/REST.php:118堆栈跟踪:#0 /vendor/google/apiclient/src/Google/Http/REST.php(94):Google_Http_REST :: decodeHttpResponse (对象(GuzzleHttp \ Psr7 \ Response),对象(GuzzleHttp \ Psr7 \ Request),' Google_Service _...')#1 /vendor/google/apiclient/src/Google/Task/Runner.php (181):Google_Http_REST :: doExecute(对象(GuzzleHttp \ Client),对象(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'
            ),
        )

3 个答案:

答案 0 :(得分:1)

幻灯片绝对可以提供更好的错误消息,但我很确定问题是您尝试插入的http://localhost:9880/wp-content/uploads/2017/02/pla23.jpg图片网址。我尝试使用该图片发出replaceAllShapesWithImage请求,并获得了503。

幻灯片通过公共互联网下载您的图片,因此localhost域无法使用。尝试在一些可公开访问的URL上托管它,或者您可以使用Google Drive来托管图像。

答案 1 :(得分:0)

我有相同的错误消息(出于完全不同的原因)。

对我来说,我等待了30秒钟,然后再次尝试了相同的操作,并且它起作用了。

似乎只是Google API的短暂中断。

答案 2 :(得分:0)

每次我发现 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;
}