如何使用服务账号发起Google Home Graph请求获取设备列表
Google Home Graph 服务账号接入相关问题
开发背景
正在开发用于控制Google Home设备的桌面应用程序,已在认证环节卡系数小时,无法顺利完成Google Home Graph的认证流程。
已完成的前置配置
已在对应Google云项目中创建服务账号(Service Account),并将服务账号对应的JSON凭证文件存入项目工作目录,当前通过如下C#代码逻辑生成接入凭证:
string[] Scopes = { HomeGraphServiceService.Scope.Homegraph }; using (FileStream stream = new FileStream("ServiceCredentials.json", FileMode.Open, FileAccess.ReadWrite)) { ServiceCred = ServiceAccountCredential.FromServiceAccountData(stream); GoogleCredential credentials = GoogleCredential.FromServiceAccountCredential(ServiceCred).CreateScoped(Scopes); var service = new HomeGraphServiceService(new BaseClientService.Initializer() { HttpClientInitializer = credentials, ApplicationName = "Test Application" }); }
当前遇到的异常问题
- 接口调用400错误问题
调用任意Home Graph相关接口时,都要求传入AgentUserId参数,不管是尝试使用服务账号凭证文件内的任意ID、还是使用服务实例自带的ApiKey填充该参数,都会触发返回400错误的异常;生成的GoogleCredential类型credentials对象中,没有可匹配该参数要求的标识。
核心疑问:如何通过已配置的服务账号发起合法请求,从Google Home Graph中获取关联的设备列表。 - OAuth授权无效作用域问题
尝试在Google项目配套的API Playground中,为当前应用添加HomeGraphServiceService.Scope.Homegraph作用域时,跳转的OAuth页面会直接返回400错误,提示“Invalid Scope”。

内容的提问来源于stack exchange,提问作者Techrex Studios
相关产品推荐
相关产品推荐

