【问题标题】:Report state to GOOGLE, The request is missing a valid API key向 GOOGLE 报告状态,请求缺少有效的 API 密钥
【发布时间】:2021-08-27 07:54:30
【问题描述】:

当我测试向谷歌报告智能家居设备状态时,我收到以下错误:

"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"status": "PERMISSION_DENIED"}

我按照此指南https://developers.google.com/assistant/smarthome/develop/report-state#service-account-key 获取服务帐户密钥。

HttpEntity entity = httpRequest("https://www.googleapis.com/auth/homegraph", jwt, params, "application/x-www-form-urlencoded");

这是我获取令牌的代码。

HttpEntity entity = httpRequest("https://homegraph.googleapis.com/v1/devices:reportStateAndNotification", token, params, "application/json");

这是我报告状态的代码。

我想知道令牌是无效还是有什么问题。

另外,这是否与agentUserId有关。

{"requestId": "123ABC","agentUserId": "user-123","payload": {
"devices": {
  "states": {
    "light-123": {
      "on": true
    }
  }
}

} }

【问题讨论】:

    标签: google-smart-home


    【解决方案1】:

    您需要在 JSON 请求中使用您的 agentUserId - 您可以使用 OAuth Playground tool 检索它。

    Account Linking guide 还解释了如何使用此工具来验证您是否已正确配置所有身份验证令牌。

    【讨论】:

      【解决方案2】:

      现在,我尝试了其他方式,但我收到了另一个错误的请求。

      {"error":"invalid_scope","error_description":"Invalid OAuth scope or ID token audience provided."}, iss: securam-report-state@securam-smart.iam.gserviceaccount.com
      

      我更改为用户 GoogleCredentials

      GoogleCredentials creds;
          FileInputStream stream = new FileInputStream("/usr/local/cloud_config/securam-smart-88f8bf5c2792.json");
          creds = GoogleCredentials.fromStream(stream);
          creds.createScoped("https://www.googleapis.com/auth/homegraph");
          creds.refreshIfExpired();
          AccessToken token = creds.getAccessToken();
          return token.getTokenValue();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 2018-07-05
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        相关资源
        最近更新 更多