【问题标题】:Google Cloud API PHP Client Authentication using Service Account使用服务帐户的 Google Cloud API PHP 客户端身份验证
【发布时间】: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


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

【讨论】:

    【解决方案2】:

    由于您创建 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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2019-04-14
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多