首页 文章

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

提问于
浏览
0

我正在使用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中的权限被拒绝第102行的.php致命错误:未捕获的异常'Google \ Cloud \ Core \ Exception \ ServiceException',消息为“{”错误“:{”errors“:[{”domain“:”global“,”reason“:”authError “,”消息“:”无效凭据“,”locationType“:” Headers “,”位置“:”授权“}],”代码“: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 .P hp(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(对象(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 in / Applications / XAMPP / xamppfiles / htdocs / storage / vendor / google / cloud-core /第263行的src / RequestWrapper.php

请帮帮我!

提前致谢!

2 回答

  • 1

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

    在您的示例中,您已使用密钥文件创建了ServiceBuilder实例,但您没有使用该实例来调用存储 .

    两种选择:

    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 都应该经过身份验证并可以使用!

  • 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());
        }
    }
    

相关问题