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

Dim*_*tar 3 php google-api google-cloud-storage google-oauth google-cloud-speech

  1. 我在这里完成了指南
  2. 我也完成了这个指南(因为我想同时使用存储和语音)

现在我有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
Run Code Online (Sandbox Code Playgroud)

它正在我正在努力:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the Brooklyn Bridge",
          "confidence": 0.9840146
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

# 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);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

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

Dim*_*tar 6

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

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

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

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

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

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

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
Run Code Online (Sandbox Code Playgroud)

这应该工作.

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

use Google\Auth\Credentials\GCECredentials;

$gceCredentials = new GCECredentials();
$config = [
    'projectId' => $projectId,
    'credentialsFetcher' => $gceCredentials,
];
Run Code Online (Sandbox Code Playgroud)