doucaigai7176 2018-10-12 09:16
浏览 268

我在哪里将api密钥放在Google Cloud PHP语言客户端中?

I am trying to perform a sentiment analysis with the Google Cloud PHP language client from this tutorial: https://cloud.google.com/natural-language/docs/reference/libraries

In the documentation they say this should work with a plain api key: https://cloud.google.com/docs/authentication/api-keys

I already tried a couple of ways on how to set the api key (plain api key, no oauth), but I am always getting the error: "The request is missing a valid API key."

Here some of my tries:

// 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'];
  • 写回答

1条回答 默认 最新

  • dqy1265 2018-11-16 15:48
    关注

    ok, I figured out how to perform a plain api call without using the client library like this: POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY

    like described here: https://cloud.google.com/natural-language/docs/reference/rest/v1/documents/analyzeEntities

    I could resolve the issue with the authentication for the client library by adding the parameter keyFilePath to the config of the LanguageClient like this:

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度