Php 该服务目前无法使用Google api

Php 该服务目前无法使用Google api,php,google-api,access-token,google-slides-api,Php,Google Api,Access Token,Google Slides Api,我正在使用google slide API连接到我的项目帐户,连接工作正常,但有时我会出现以下错误: 致命错误:未捕获的谷歌服务异常:{“错误”:{“代码”:503,“消息”:“服务当前不可用”,“错误”:[{“消息”:“服务当前不可用”,“域”:“全局”,“原因”:“backendError”}],“状态”:“不可用”}在/vendor/google/apiclient/src/google/Http/REST.php:118堆栈跟踪:#0/vendor/google/apiclient/sr

我正在使用google slide API连接到我的项目帐户,连接工作正常,但有时我会出现以下错误:

致命错误:未捕获的谷歌服务异常:{“错误”:{“代码”:503,“消息”:“服务当前不可用”,“错误”:[{“消息”:“服务当前不可用”,“域”:“全局”,“原因”:“backendError”}],“状态”:“不可用”}在/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####服务…”)#1/vendor/google/google/apiclient/src/google/Task/Runner.php(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\u 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'
            ),
        )

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


幻灯片通过公共internet下载图像,因此
localhost
域无法工作。请尝试将其托管在可公开访问的URL上,或者您可以使用来托管图像。

我收到了相同的错误消息(原因完全不同)

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


似乎这可能只是谷歌API的短暂中断。

每次我发现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;
}
公共函数获取分析响应()
{
$api_call_num_of_尝试=5;
$api_调用_尝试次数=0;
$api_call_sleep=1;
做{
试一试{
$response=$this->analytics->reports->batchGet();
}
捕获(谷歌服务例外$e){
//睡眠1秒、2秒、4秒、8秒、16秒+随机毫秒
$sleep=($api_call_sleep*1000000)+兰特(10001000000);
usleep(睡眠);
$api_调用_++;
$api_call_sleep=$api_call_sleep*2;
$error_message_json=$e->getMessage();
$error\u message=json\u decode($error\u message\u json,true);
//保存错误。。。
继续;
}
打破
}
而($api_调用尝试次数<$api_调用尝试次数);
返回$response;
}

哪一行抛出错误?我更新了我的问题,您可以看到行error@MALKIMOHAMED你能提供导致503的请求主体吗?你粘贴的代码只执行oauth流,不进行任何幻灯片API调用。我同意@MauriceCodik请提供一个ok@MauriceCodik我再次更新我的问题以添加e请求