完成Account linking后,如何获取Google Home App全品类绑定设备列表?
实现方案:使用Google Home Graph API
Google Home Graph是Google存储和管理用户智能家居设备状态的核心服务,通过它可以获取用户Google Home账号下所有已关联的设备(包括Nest、Tuya、Philips等第三方品牌设备),前提是你的应用已经完成与Google Home的Account Linking操作。
核心API接口
1. devices.query 接口
这是获取设备列表的核心接口,调用后会返回用户账号下所有同步到Home Graph的设备信息,包含设备类型、状态、品牌归属等关键数据。
调用前提
- 需通过OAuth 2.0完成授权,授权范围必须包含
https://www.googleapis.com/auth/homegraph - 请求需携带用户通过Account Linking获取的有效访问令牌
后端实现示例(Node.js)
const { google } = require('googleapis'); // 初始化OAuth2客户端 const oauth2Client = new google.auth.OAuth2( '你的应用Client ID', '你的应用Client Secret' ); // 注入用户授权令牌(Account Linking流程获取) oauth2Client.setCredentials({ access_token: '用户访问令牌', refresh_token: '用户刷新令牌' }); // 初始化Home Graph API客户端 const homegraph = google.homegraph({ version: 'v1', auth: oauth2Client }); // 获取全量设备列表的函数 async function fetchAllHomeDevices() { try { const response = await homegraph.devices.query({ requestBody: { agentUserId: '你的应用对用户的唯一标识', inputs: [{ intent: 'action.devices.QUERY' }] } }); return response.data.payload.devices; } catch (err) { console.error('获取设备失败:', err.message); throw err; } } // 执行调用 fetchAllHomeDevices().then(devices => console.log('全量设备列表:', devices));
返回数据片段示例
{ "devices": [ { "id": "nest_thermo_001", "type": "action.devices.types.THERMOSTAT", "traits": ["action.devices.traits.TemperatureSetting"], "name": { "name": "客厅温控器" }, "deviceInfo": { "manufacturer": "Google Nest", "model": "Thermostat E" }, "status": "SUCCESS" }, { "id": "philips_hue_bulb_007", "type": "action.devices.types.LIGHT", "traits": ["action.devices.traits.OnOff", "action.devices.traits.Brightness"], "name": { "name": "卧室氛围灯" }, "deviceInfo": { "manufacturer": "Philips", "model": "Hue White Ambiance" }, "status": "SUCCESS" } ] }
2. devices.sync 接口
如果需要强制同步最新的设备数据(比如用户刚新增了第三方设备),可以调用devices.sync接口,它会返回用户账号下所有设备的完整配置信息,包括支持的功能、设备属性等。
关键注意事项
- 必须在Google Cloud Console中启用Home Graph API
- Account Linking流程中,需明确申请
https://www.googleapis.com/auth/homegraph权限范围 - 若访问令牌过期,需使用刷新令牌重新获取有效令牌
内容的提问来源于stack exchange,提问作者Sandro Henriques
相关产品推荐
相关产品推荐

