现在我有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
所以我的问题是我做错了什么以及为什么这段代码不起作用?我在其他计算机上做了相同的代码,它工作正常,我不明白为什么它不能以同样的方式在这台计算机上工作?任何建议都会受到欢迎!
答案 0 :(得分: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 :(得分:1)
我突然遇到了同样的情况,试图访问我的一个GCP存储桶(特别是尝试使用401 Invalid Credentials
从我的一个存储桶中传输文件时,我得到了gsutil
)
我终于可以通过删除存储桶,创建一个新的存储桶,然后重新上传我的文件夹来解决这个问题。
答案 2 :(得分: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')
);