【发布时间】:2019-01-10 06:02:04
【问题描述】:
我正在处理一个使用 PHP 的项目,需要使用 PHP 客户端库实现 Google Cloud API,但身份验证似乎对我不起作用。 我创建了一个服务帐户并授予项目所有者权限,我不想使用 GOOGLE_DEFAULT_CREDENTIALS 环境变量进行身份验证,我想使用服务帐户身份验证。
这是我尝试过的:
require 'vendor/autoload.php';
use Google\Cloud\Core\ServiceBuilder;
use Google\Cloud\Storage\StorageClient;
// Authentication with Google Cloud Platform
$client = new ServiceBuilder([
'keyFilePath' => 'api-project-374381085870-eaf930d9ffd7.json'
]);
$client = new StorageClient();
$bucket = $client->bucket('storage_client');
// Upload a file to the bucket.
$bucket->upload(
fopen('file.txt', 'r')
);
但它返回错误为:
警告:file_get_contents(/Users/abdul/.config/gcloud/application_default_credentials.json):无法打开流:/Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/auth/src/CredentialsLoader 中的权限被拒绝.php 在第 102 行
警告: 文件获取内容(/Users/abdul/.config/gcloud/application_default_credentials.json): 无法打开流:权限被拒绝 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/auth/src/CredentialsLoader.php 在第 102 行
致命错误:未捕获的异常 带有消息“{”的“Google\Cloud\Core\Exception\ServiceException” “错误”:{“错误”:[{“域”:“全局”,“原因”:“authError”, “消息”:“无效凭据”,“locationType”:“标题”, “位置”:“授权”}],“代码”:401,“消息”:“无效 凭据" } } ' 在 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/RequestWrapper.php:263 堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/RequestWrapper.php(168): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException))
1 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/Upload/MultipartUploader.php(65):
Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), 阵列)#2 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-storage/src/Bucket.php(283): Google\Cloud\Core\Upload\MultipartUploader->upload() #3 /Applications/XAMPP/xamppf 在 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/RequestWrapper.php 在第 263 行
请帮帮我!
提前致谢!
【问题讨论】:
-
将您的
keyFilePath提供给您实际使用的课程。ServiceBuilder实例化在这里是不必要的。new StorageClient(['keyFilePath' => '...']); -
嗨@jdp,谢谢,它正在工作......如果你愿意,你可以发布答案。
-
太棒了!很高兴它奏效了。 :)
标签: php xampp google-cloud-platform google-api-php-client google-authentication