报告状态因未经授权而失败,Google 主页操作

report state fails with unauthorized, Google Home action

我正在 onExecute 处理程序中发送 report state 请求。它失败了:

io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
   at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:233)
   at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:214)
   at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:139)
   at com.google.home.graph.v1.HomeGraphApiServiceGrpc$HomeGraphApiServiceBlockingStub.reportStateAndNotification(HomeGraphApiServiceGrpc.java:425)
   at com.google.actions.api.smarthome.SmartHomeApp.reportState(SmartHomeApp.kt:132)
[snip]

我不明白这是怎么可能的,因为我使用 SmartHomeApp 发出请求,接收同步、查询、执行、断开连接的同一个 SmartHomeApp。这是我的代码:

var state = new HashMap();
state.put("openPercent", 50); // etc
SmartHomeApp app = ... // this is what received the onExecute
app.reportState(ReportStateAndNotificationRequest.newBuilder()
    .setRequestId(request.requestId) // onExecute requestId
    .setAgentUserId(userId) // hardcoded user
    .setPayload(StateAndNotificationPayload.newBuilder()
        .setDevices(ReportStateAndNotificationDevice.newBuilder()
            .setStates(Struct.newBuilder()
                // toValue converts the state to a protobuf value, see data below
                .putFields("kitchenDoor", UtilProtoBuf.toValue(state))
                .build()
            ).build()
        ).build()
    ).build());

toStringReportStateAndNotificationRequest所以可以看到数据:

request_id: "16744804305948869781"
agent_user_id: "123"
payload {
  devices {
    states {
      fields {
        key: "kitchenDoor"
        value {
          struct_value {
            fields {
              key: "openPercent"
              value {
                number_value: 50.0
              }
            }
          }
        }
      }
    }
  }
}

虽然您可以接收入站请求,但您无法立即将出站请求返回 Google。首先你必须 activate the HomeGraph API and download a service account key.

然后将其添加到您的 SmartHomeApp:

  // Get service account key from file
  FileInputStream stream = new FileInputStream("service-account-key.json");
  GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
  mySmartHomeApp.setCredentials(credentials);

这应该可以解决您的授权问题。