gpt4 book ai didi

php - 尝试连接时,Google Cloud Storage 向我提供授权代码 401 无效凭据

转载 作者:行者123 更新时间:2023-12-02 06:41:59 25 4
gpt4 key购买 nike

  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

所以我的问题是我做错了什么以及为什么这段代码不起作用?我在其他计算机上执行了相同的代码并且工作正常,我不明白为什么它不能以相同的方式在这台计算机上工作?如有任何建议,我们将非常欢迎!

最佳答案

2022 年更新

$config = [
'credentials' => [PATH-TO-JSON-FILE],
];

$client = new BetaAnalyticsDataClient($config);

2018 年更新

use Google\Auth\Credentials\GCECredentials;

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

我注意到,当您想要实例化 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');

这应该有效。

关于php - 尝试连接时,Google Cloud Storage 向我提供授权代码 401 无效凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48880471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com