NateS
NateS

Reputation: 5876

report state fails with unauthorized, Google Home action

I'm sending a report state request in my onExecute handler. It fails with:

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]

I don't see how this is possible since I use the SmartHomeApp to make the request, the same SmartHomeApp that receives sync, query, execute, disconnect. Here's my code:

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());

The toString for the ReportStateAndNotificationRequest so you can see the data:

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

Upvotes: 0

Views: 93

Answers (1)

Nick Felker
Nick Felker

Reputation: 11978

Though you can receive inbound requests, you are not immediately able to make outbound requests back to Google. First you must activate the HomeGraph API and download a service account key.

Then add that to your SmartHomeApp:

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

That should resolve your authorization issue.

Upvotes: 1

Related Questions