我应该把我的 api 密钥放在 Google Cloud PHP Language Client 中的什么位置?



我正在尝试使用本教程中的Google Cloud PHP语言客户端执行情绪分析:https://cloud.google.com/natural-language/docs/reference/libraries

在文档中,他们说这应该与普通的 api 密钥一起使用:https://cloud.google.com/docs/authentication/api-keys

我已经尝试了几种如何设置 api 密钥的方法(纯 api 密钥,没有 oauth(,但我总是收到错误:"请求缺少有效的 API 密钥。

这是我的一些尝试:

// Instantiates a client
$language = new LanguageClient([
'projectId' => $projectId,
'key' => $key,
'developerKey' => $key,
'api_key' => $key
]);
$language->setDeveloperKey($key);
// Detects the sentiment of the text
$annotation = $language->analyzeSentiment($texttoanalyze);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . 'Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];

好的,我想出了如何在不使用客户端库的情况下执行普通 api 调用,如下所示: 发布 https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY

如下所述: https://cloud.google.com/natural-language/docs/reference/rest/v1/documents/analyzeEntities

我可以通过将参数 keyFilePath 添加到 LanguageClient 的配置来解决客户端库身份验证的问题,如下所示:

$language = new LanguageClient([
'projectId' => 'my-project-id',
'keyFilePath' => '/path/to/my/keyfile.json'
]);

最新更新