You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过.NET应用调用Google API获取Google Home已连接设备列表

解决方案:通过SDM API获取Google Home已连接设备列表及状态

你之前的方向有误——不需要在Actions Console创建Action,也不用SYNC/QUERY意图,这些是给第三方智能家居设备开发者用来对接Google Home的。要读取自己Google Home下的设备,应该用Google的Smart Device Management (SDM) API。

核心步骤修正

1. 确认权限范围

必须使用SDM专属的权限范围:https://www.googleapis.com/auth/sdm.service,替换你之前的智能管理范围,这个范围才允许读取设备列表和状态。

2. 正确的API调用流程

SDM API的设备是按「结构(Structure)」组织的(比如你的家、各个房间),需要先获取结构列表,再获取每个结构下的设备,最后查询设备状态:

步骤1:创建SDM服务实例

修正你之前的服务实例创建代码,要针对SDM API:

using Google.Apis.SmartDeviceManagement.v1;
using Google.Apis.Auth.OAuth2;

// 用用户授权后的Credential创建SDM服务
var service = new SmartDeviceManagementService(new BaseClientService.Initializer
{
    HttpClientInitializer = credential, // 这里用你通过OAuth 2.0获取的用户Credential,不是服务账号凭证
    ApplicationName = "你的应用名称"
});

注意:这里必须用用户授权后的Credential(通过OAuth 2.0获取的Access Token对应的Credential),服务账号凭证无法访问个人用户的Google Home设备,因为SDM API需要用户的个人授权。

步骤2:获取结构列表

调用Structures.List接口获取用户的所有结构:

var structuresListRequest = service.Structures.List();
structuresListRequest.PageSize = 10; // 根据需要调整
var structuresResponse = await structuresListRequest.ExecuteAsync();

foreach (var structure in structuresResponse.Structures)
{
    Console.WriteLine($"结构ID: {structure.Name},名称: {structure.Traits["sdm.structures.traits.Info"].Name}");
    // 下一步获取该结构下的设备
    await GetDevicesInStructure(service, structure.Name);
}

步骤3:获取结构下的设备列表

调用Devices.List接口,传入结构ID作为父资源:

private async Task GetDevicesInStructure(SmartDeviceManagementService service, string structureName)
{
    var devicesListRequest = service.Devices.List();
    devicesListRequest.Parent = structureName; // 格式类似 "enterprises/[ENTERPRISE_ID]/structures/[STRUCTURE_ID]"
    devicesListRequest.PageSize = 20;
    var devicesResponse = await devicesListRequest.ExecuteAsync();

    foreach (var device in devicesResponse.Devices)
    {
        Console.WriteLine($"设备ID: {device.Name},类型: {device.Type}");
        // 获取设备状态
        await GetDeviceState(service, device.Name);
    }
}

步骤4:获取设备状态

调用Devices.Get接口获取设备的详细状态和属性:

private async Task GetDeviceState(SmartDeviceManagementService service, string deviceName)
{
    var deviceRequest = service.Devices.Get(deviceName);
    var device = await deviceRequest.ExecuteAsync();

    // 输出设备状态,比如灯泡的亮度、开关状态,烟雾传感器的报警状态等
    Console.WriteLine("设备状态:");
    foreach (var trait in device.Traits)
    {
        Console.WriteLine($"- {trait.Key}: {JsonConvert.SerializeObject(trait.Value)}");
    }
}

关键疑问解答

关于AgentUserID

这个参数是Actions on Google框架里的概念,和SDM API无关,你完全不需要理会它——因为你不是在开发智能家居Skill,只是读取自己的设备数据。

关于Actions Console

不需要创建任何Action,Actions Console是用来开发可以通过Google Assistant语音控制的第三方技能,和你读取自己设备的需求不相关。

补充资源

可以参考Google官方的SDM API文档中的.NET代码示例,或者搜索「Smart Device Management API .NET sample」获取现成的代码片段,帮助你快速完成集成。

内容的提问来源于stack exchange,提问作者Ajay Rathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 20:05:14