我正在尝试通过本教程https://cloud.google.com/natural-language/docs/reference/libraries
与Google Cloud PHP语言客户端进行情感分析。他们在文档中说这应该与简单的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'];
答案 0 :(得分:0)
好的,我弄清楚了如何在不使用客户端库的情况下执行普通的api调用: POST 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'
]);