Google Cloud Storage 在尝试连接时向我提供授权代码 401 无效凭据
Google Cloud Storage is giving me Authorization code 401 Invalid Credentials when trying to connect
现在我有 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
所以我的问题是我做错了什么以及为什么这段代码不起作用?我在其他计算机上执行了相同的代码并且工作正常,我不明白为什么它不能以相同的方式在这台计算机上工作?欢迎任何建议!
我注意到,当您想实例化一个 Google 服务时,您只需将 JSON 文件放入数组中,它应该可以工作。例如,如果您想实例化 Google 语音客户端,只需执行以下操作:
$speech = new SpeechClient([
'projectId' => $projectId,
'keyFilePath' => 'path/to/file.json',
'languageCode' => 'en-US',
]);
如果您想要 Google 存储客户端,则只需:
$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 的帮助
更新:
如果上述 none 适合您,请尝试使用以下代码在您的文件中设置环境:
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
这应该有效。
另外,从 2018 年 12 月开始,您可以使用更新后的代码:
use Google\Auth\Credentials\GCECredentials;
$gceCredentials = new GCECredentials();
$config = [
'projectId' => $projectId,
'credentialsFetcher' => $gceCredentials,
];
我 运行 突然尝试访问我的一个 GCP 存储桶(具体来说,当我尝试使用 gsutil
传输文件时我得到 401 Invalid Credentials
来自我的一个桶)。
我终于能够通过简单地删除存储桶,创建一个新存储桶,然后重新上传我的文件夹来绕过,这在第一次运行时就成功了。
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')
);
现在我有 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
所以我的问题是我做错了什么以及为什么这段代码不起作用?我在其他计算机上执行了相同的代码并且工作正常,我不明白为什么它不能以相同的方式在这台计算机上工作?欢迎任何建议!
我注意到,当您想实例化一个 Google 服务时,您只需将 JSON 文件放入数组中,它应该可以工作。例如,如果您想实例化 Google 语音客户端,只需执行以下操作:
$speech = new SpeechClient([
'projectId' => $projectId,
'keyFilePath' => 'path/to/file.json',
'languageCode' => 'en-US',
]);
如果您想要 Google 存储客户端,则只需:
$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 的帮助
更新: 如果上述 none 适合您,请尝试使用以下代码在您的文件中设置环境:
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
这应该有效。
另外,从 2018 年 12 月开始,您可以使用更新后的代码:
use Google\Auth\Credentials\GCECredentials;
$gceCredentials = new GCECredentials();
$config = [
'projectId' => $projectId,
'credentialsFetcher' => $gceCredentials,
];
我 运行 突然尝试访问我的一个 GCP 存储桶(具体来说,当我尝试使用 gsutil
传输文件时我得到 401 Invalid Credentials
来自我的一个桶)。
我终于能够通过简单地删除存储桶,创建一个新存储桶,然后重新上传我的文件夹来绕过,这在第一次运行时就成功了。
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')
);