尝试连接时,Google云端存储会向我提供授权码401无效凭据

问题描述 投票:1回答:2
  1. 我已完成指南here
  2. 我也完成了this指南(因为我想同时使用存储和语音)

现在我有gcloud,它正在终端工作,我也尝试从shell执行此命令:

curl -s -H "Content-Type: application/json" \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    https://speech.googleapis.com/v1/speech:recognize \
    -d @sync-request.json

它正在我正在努力:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the Brooklyn Bridge",
          "confidence": 0.9840146
        }
      ]
    }
  ]
}

但是当我尝试放一些代码时。例如这一个:

# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

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

function auth_cloud_implicit($projectId)
{
    $config = [
        'projectId' => $projectId,
    ];

    # If you don't specify credentials when constructing the client, the
    # client library will look for credentials in the environment.
    $storage = new StorageClient($config);

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

# Your Google Cloud Platform project ID
$projectId = 'somenumbersandletters';

auth_cloud_implicit($projectId);

我收到此错误:

Google\Cloud\Core\Exception\ServiceException: { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } in /var/www/speech/vendor/google/cloud-core/RequestWrapper.php on line 263

所以我的问题是我做错了什么以及为什么这段代码不起作用?我在其他计算机上做了相同的代码,它工作正常,我不明白为什么它不能以同样的方式在这台计算机上工作?任何建议都会受到欢迎!

php google-api google-cloud-storage google-oauth google-cloud-speech
2个回答
6
投票

我注意到的是,当您想要实例化Google服务时,您只需将json文件放在数组中即可。例如,如果您想要实例化Google语音客户端,只需执行以下操作:

$speech = new SpeechClient([
    'projectId' => $projectId,
    'keyFilePath' => 'path/to/file.json',
    'languageCode' => 'en-US',
]);

如果您想要Google Storage Client,那么只需:

$storage = new StorageClient([
   'projectId' => $projectId,
   'keyFilePath' => 'path/to/file.json',   
])

如果这没有帮助,那么也尝试

转到此link并使用此代码连接到存储桶:

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

并且还要访问Google Cloud Platform / IAM并添加服务帐户,该代码提供的错误是该帐户无权访问该存储桶。现在一切正常!感谢@ brandon-yarbrough的帮助

更新:如果以上都不适合您,只需使用以下代码尝试在文件中设置环境:

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');

这应该工作。

从12.2018开始,您可以使用更新的代码:

use Google\Auth\Credentials\GCECredentials;

$gceCredentials = new GCECredentials();
$config = [
    'projectId' => $projectId,
    'credentialsFetcher' => $gceCredentials,
];

1
投票

我突然试图访问我的一个GCP存储桶(特别是我在尝试使用401 Invalid Credentials从我的一个存储桶传输文件时得到gsutil)。

我终于可以通过简单地删除桶,创建一个新的,然后重新上传我的文件夹,这是第一次工作。


0
投票
require 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;       
 $storage = new StorageClient([
        'keyFilePath' => 'path/key.json',
        'projectId' => 'you_projectid'
    ]);
    $bucket = $storage->bucket('namebucket');
    $bucket->upload(
        fopen('data/demo.txt', 'r')
    );
© www.soinside.com 2019 - 2024. All rights reserved.