使用HomeGraphService API实现StateReporting无响应问题求助
Google Home Graph StateReporting 调用无响应问题排查
我正在为Google Action实现StateReporting功能,基于HomeGraphService API开发,已遵循官方文档步骤。现在遇到的问题是:调用reportStateAndNotification函数后,仅返回一个空的com.google.api.services.homegraph.v1.HomeGraphService$Devices$ReportStateAndNotification对象,HomeGraphViewer中的设备状态也没有更新。但用credentials.accessToken获取的令牌通过curl调用相同接口时,功能完全正常。
代码实现
val stream = Base64.getDecoder().decode(homeGraphServiceKey) val credentials = GoogleCredentials.fromStream(stream.inputStream()) .createScoped("https://www.googleapis.com/auth/homegraph") credentials.refreshIfExpired() val token = credentials.accessToken val service = HomeGraphService.Builder( GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), HttpCredentialsAdapter(credentials) ) .setApplicationName("ff") .build() val states = mutableMapOf<String, MutableMap<String, Any>>() val state = mutableMapOf<String, Any>() state["on"] = true states["hue:cE1"] = state val request = ReportStateAndNotificationRequest().setRequestId( kotlin.random.Random.nextLong().toString() ) .setAgentUserId("7b1d52e3-e68f-46cd-a8b9-6ce1fdfe5b62").setPayload( StateAndNotificationPayload().setDevices( ReportStateAndNotificationDevice().setStates( states.toMap() ) ) ) val response = service?.devices()?.reportStateAndNotification(request)
curl调用示例
请求JSON:
{ "requestId": "123ABC", "agentUserId": "7b1d52e3-e68f-46cd-a8b9-6ce1fdfe5b62", "payload": { "devices": { "states": { "hue:cE1": { "on": true } } } } }
curl命令:
curl -X POST -H "Authorization: Bearer token" -H "Content-Type: application/json" -d @request "https://homegraph.googleapis.com/v1/devices:reportStateAndNotification"
核心问题与解决方案
问题根源
Google的Java/Kotlin客户端库中,reportStateAndNotification(request)仅构建了请求对象,并未实际发送HTTP请求。你需要调用.execute()方法才会触发请求发送,否则返回的只是未执行的请求构建器对象,而非实际响应。
修改后的代码
val stream = Base64.getDecoder().decode(homeGraphServiceKey) val credentials = GoogleCredentials.fromStream(stream.inputStream()) .createScoped("https://www.googleapis.com/auth/homegraph") credentials.refreshIfExpired() val token = credentials.accessToken val service = HomeGraphService.Builder( GoogleNetHttpTransport.newTrustedTransport(), GsonFactory.getDefaultInstance(), HttpCredentialsAdapter(credentials) ) .setApplicationName("ff") .build() val states = mutableMapOf<String, MutableMap<String, Any>>() val state = mutableMapOf<String, Any>() state["on"] = true states["hue:cE1"] = state val request = ReportStateAndNotificationRequest() .setRequestId(kotlin.random.Random.nextLong().toString()) .setAgentUserId("7b1d52e3-e68f-46cd-a8b9-6ce1fdfe5b62") .setPayload( StateAndNotificationPayload() .setDevices( ReportStateAndNotificationDevice() .setStates(states.toMap()) ) ) // 关键:添加execute()方法执行实际请求 val response = service?.devices()?.reportStateAndNotification(request)?.execute() // 可选:打印响应日志辅助排查 println("Response: ${response?.toPrettyString()}")
额外排查点
- 确认请求结构:打印客户端构建的请求JSON,确保和curl使用的请求完全一致
- 权限验证:检查服务账号是否拥有Home Graph API的调用权限,scope是否正确设置
- 请求ID唯一性:确保每次请求的requestId唯一,避免重复请求被Google Home Graph忽略
内容的提问来源于stack exchange,提问作者shalama
相关产品推荐
相关产品推荐

