Mal*_*med 9 php google-api access-token google-slides-api
我正在使用 google Slide API 连接到我的项目帐户,连接工作正常,但有时我会收到此错误:
致命错误:未捕获的 Google_Service_Exception: { "error": { "code": 503, "message": "该服务当前不可用。", "errors": [ { "message": "该服务当前不可用。", /vendor/google/apiclient/src/Google/Http/REST.php:118 中的“domain”:“global”,“reason”:“backendError” } ],“status”:“UNAVAILABLE” } } 堆栈跟踪:# 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->在 /vendor/google/apiclient/src/Google/Http/REST.php 第 118 行运行
这是我的 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;
}
Run Code Online (Sandbox Code Playgroud)
这是我发送的请求:
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;
}
Run Code Online (Sandbox Code Playgroud)
$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'
),
)
Run Code Online (Sandbox Code Playgroud)
幻灯片肯定可以提供更好的错误消息,但我很确定问题在于http://localhost:9880/wp-content/uploads/2017/02/pla23.jpg您尝试插入的图像 URL。我尝试使用replaceAllShapesWithImage该图像发出请求,也得到了 503。
幻灯片通过公共互联网下载您的图像,因此localhost域不起作用。尝试将其托管在一些可公开访问的 URL 上,或者您可以使用Google 云端硬盘来托管图像。