使用服务帐户的Google Cloud API PHP客户端身份验证

时间:2018-08-02 12:46:45

标签: php xampp google-cloud-platform google-api-php-client google-authentication

我正在使用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

     

警告:   file_get_contents(/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”,   “ message”:“无效的凭据”,“ locationType”:“标题”,   “ location”:“授权”}],“代码”:401,“ message”:“无效   凭证“}}'in   /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行

请帮帮我!

谢谢!

2 个答案:

答案 0 :(得分:1)

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

在您的示例中,您已经创建了一个带有密钥文件的ServiceBuilder实例,但没有使用该实例来调用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都应经过身份验证并可以使用!

答案 1 :(得分:0)

由于创建StorageClient对象和指定私钥文件参数的方式而产生了此问题。

您可以在Google Cloud Platform网站上找到Pass the path to the service account key in code的以下示例,该示例对于您的问题非常有用:

namespace Google\Cloud\Samples\Auth;

// Imports the Google Cloud Storage client library.
use Google\Cloud\Storage\StorageClient;

function auth_cloud_explicit($projectId, $serviceAccountPath)
{
    # Explicitly use service account credentials by specifying the private key
    # file.
    $config = [
        'keyFilePath' => $serviceAccountPath,
        'projectId' => $projectId,
    ];
    $storage = new StorageClient($config);

    # Make an authenticated API request (listing storage buckets)
    foreach ($storage->buckets() as $bucket) {
        printf('Bucket: %s' . PHP_EOL, $bucket->name());
    }
}