Skip to content

Instantly share code, notes, and snippets.

@NodeJSmith
Last active January 30, 2025 00:44
Show Gist options
  • Save NodeJSmith/72bf858816bb4a36548a28dcb03cc5d6 to your computer and use it in GitHub Desktop.
Save NodeJSmith/72bf858816bb4a36548a28dcb03cc5d6 to your computer and use it in GitHub Desktop.
Get device IDs from Nest/SDM api
# Written for this comment/issue: https://github.com/AlexxIT/go2rtc/issues/1440#issuecomment-2568140358
# Using AlexxIT's nest api Go code: https://github.com/AlexxIT/go2rtc/blob/master/pkg/nest/api.go
import os
import requests
get_access_token_url = "https://www.googleapis.com/oauth2/v4/token"
client_id = os.environ.get("NEST_CLIENT_ID")
client_secret = os.environ.get("NEST_CLIENT_SECRET")
refresh_token = os.environ.get("NEST_REFRESH_TOKEN")
project_id = os.environ.get("NEST_PROJECT_ID")
data = {
"grant_type": "refresh_token",
"client_id": client_id,
"client_secret": client_secret,
"refresh_token": refresh_token,
}
res = requests.post(get_access_token_url, data=data)
resv = res.json()
access_token = resv["access_token"]
get_devices_url = f"https://smartdevicemanagement.googleapis.com/v1/enterprises/{project_id}/devices"
headers = {"Authorization": f"Bearer {access_token}"}
res = requests.get(get_devices_url, headers=headers)
device_data = res.json()
replace_value = f"enterprises/{project_id}/devices/"
for device in device_data["devices"]:
device_type = device["type"]
if device_type != "sdm.devices.types.CAMERA":
continue
display_name = device["parentRelations"][0]["displayName"]
device_id = device["name"]
device_id = device_id.replace(replace_value, "")
print(f"Device Name: {display_name!r}, Device ID: {device_id!r}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment