使用服务帐户的Google Cloud API PHP客户端身份验证 [英] Google Cloud API PHP Client Authentication using Service Account

查看:163
本文介绍了使用服务帐户的Google Cloud API PHP客户端身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP进行项目,需要使用PHP客户端库实现Google Cloud API,但是身份验证似乎对我不起作用. 我已经创建了一个服务帐户并授予了项目所有者权限,并且我不想使用GOOGLE_DEFAULT_CREDENTIALS环境变量进行身份验证,我想使用服务帐户身份验证.

I'm working with a project using PHP and need to implement Google Cloud APIs using PHP Client library, but the authentication does not seem to be working for me. I have created a service account and granted the project owner permissions and I don't want to make authentication by using the GOOGLE_DEFAULT_CREDENTIALS environment variable, I want to use service account authentication.

这是我尝试过的:

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

Warning: file_get_contents(/Users/abdul/.config/gcloud/application_default_credentials.json): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/auth/src/CredentialsLoader.php on line 102

警告: file_get_contents(/Users/abdul/.config/gcloud/application_default_credentials.json): 无法打开流:权限被拒绝 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/auth/src/CredentialsLoader.php 在第102行

Warning: file_get_contents(/Users/abdul/.config/gcloud/application_default_credentials.json): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/auth/src/CredentialsLoader.php on line 102

致命错误:未捕获的异常 带有消息"{ 错误":{错误":[{域":全局",原因":"authError", "message":无效凭据","locationType":"header", 位置":授权"; }],代码":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))

Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message '{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } ' in /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/RequestWrapper.php:263 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/RequestWrapper.php(168): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException))

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行

Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-storage/src/Bucket.php(283): Google\Cloud\Core\Upload\MultipartUploader->upload() #3 /Applications/XAMPP/xamppf in /Applications/XAMPP/xamppfiles/htdocs/storage/vendor/google/cloud-core/src/RequestWrapper.php on line 263

请帮帮我!

提前谢谢!

推荐答案

必须将密钥文件配置提供给正在调用的客户端. ServiceBuilder通常很方便,因为它允许您使用配置创建单个实例,并将该配置传递给每个新客户端.

The keyfile configuration must be provided to the client which is being called. The ServiceBuilder is often convenient because it allows you to create a single instance with your configuration, and that configuration is passed to each new client.

在您的示例中,您已经创建了一个带有密钥文件的ServiceBuilder实例,但没有使用该实例来调用Storage.

In your example, you've created a ServiceBuilder instance with a keyfile, but you're not using that instance to call Storage.

两个选项:

use Google\Cloud\Core\ServiceBuilder;

$cloud = new ServiceBuilder([
    'keyFilePath' => 'my-keyfile.json'
]);

$storage = $cloud->storage();

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient([
    'keyFilePath' => 'my-keyfile.json'
]);

在两个示例中,$storage应该都经过身份验证并可以使用!

In both examples, $storage should be authenticated and ready to use!

这篇关于使用服务帐户的Google Cloud API PHP客户端身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆