Google Device Access API 404错误:Nest摄像头无法获取直播流
解决Google Nest Cam(电池版)SDM API 404 NOT_FOUND错误
调用SDM API获取摄像头数据时返回404 NOT_FOUND,核心原因是请求的设备实体无法被API识别,以下是针对性排查和解决步骤:
1. 修正API请求URL的核心参数
你的代码中PROJECT-ID是占位符,必须替换为Device Access控制台中的企业ID(不是Google Cloud项目ID)。SDM API的正确URL格式为:
https://smartdevicemanagement.googleapis.com/v1/enterprises/{企业ID}/devices/{设备ID}
- 登录Device Access控制台,在"Manage product"页面可以找到企业ID,格式类似
enterprises/XXXXXX,仅需将XXXXXX部分填入URL即可。
2. 确认设备ID的有效性
不要凭记忆填写设备ID,必须通过SDM API的设备列表接口获取真实存在的设备ID:
$enterprise_id = "你的Device Access企业ID"; $list_url = "https://smartdevicemanagement.googleapis.com/v1/enterprises/{$enterprise_id}/devices"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $list_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer {$access_token}" ]); $response = curl_exec($ch); $devices = json_decode($response, true); print_r($devices); curl_close($ch);
执行这段代码后,检查返回的设备列表,找到目标Nest Cam对应的name字段(格式为enterprises/XXX/devices/XXX),字段后半段的设备ID才是正确的device_id值。
3. 验证Access Token的权限与有效性
- 检查Access Token是否包含
sdm.service权限:在本地执行以下请求(替换你的token),查看scope字段是否包含https://www.googleapis.com/auth/sdm.service:curl https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=你的AccessToken - 如果token过期,用Refresh Token重新获取新的Access Token,确保请求时使用的是最新有效token。
4. 确认设备已绑定到Device Access项目
登录Device Access控制台,进入设备管理页面,确认你的Nest Cam已经完成账号授权并关联到当前项目。如果设备未被添加,API无法检索到它。
修正后的完整代码示例
$client_id = "你的Google Cloud Client ID"; $client_secret = "你的Google Cloud Client Secret"; $device_id = "从设备列表接口获取的真实设备ID"; $redirect_uri = "http://localhost"; $access_token = "有效Access Token"; $refresh_token = "你的Refresh Token"; $enterprise_id = "你的Device Access企业ID"; // 获取设备基本信息 $camera_url = "https://smartdevicemanagement.googleapis.com/v1/enterprises/{$enterprise_id}/devices/{$device_id}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $camera_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer {$access_token}" ]); $response = curl_exec($ch); if (curl_errno($ch)) { echo 'cURL错误 : ' . curl_error($ch); } else { $camera_response = json_decode($response, true); } curl_close($ch); print_r($camera_response);
额外:获取直播流地址
成功获取设备信息后,需要调用命令接口生成RTSP直播流:
$command_url = "https://smartdevicemanagement.googleapis.com/v1/enterprises/{$enterprise_id}/devices/{$device_id}:executeCommand"; $post_data = json_encode([ "command" => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream", "params" => [] ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $command_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer {$access_token}", "Content-Type: application/json" ]); $response = curl_exec($ch); $stream_response = json_decode($response, true); print_r($stream_response); curl_close($ch);
返回结果中的streamUri字段就是直播流地址,注意该地址有过期时间,需要定期重新生成。
内容的提问来源于stack exchange,提问作者Eren TURKOGLU
相关产品推荐
相关产品推荐

