Google Admin Directory API-通过APICLIENT发送查询



我正在使用该设备的序列号作为参考,通过Google Admin Directory API检索Chromeos设备MAC地址,并通过 apiclient

service = discovery.build('admin', 'directory_v1', developerKey=settings.API_KEY)

以下是Chromeos设备的通话;我的问题是我需要设备ID才能执行以下操作:

service.chromeosdevices().get(customerId=settings.CID, deviceId=obtained_id, projection=None).execute()

我可以通过以下格式发送获取查询:

https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/chromeos?projection=full&query=id:" + serial + "&orderBy=status&sortOrder=ascending&maxResults=10", "GET")

...但是我试图避免使用oauth2并使用我的API键。在GET请求中传递key也不起作用,因为它仍然返回"登录必需" 注意。

如何将上述查询挤入apiclient友好格式中?我通过上述呼叫找到的唯一选项是请求我们拥有的每个设备(通过list),然后筛选匹配序列号的数据山,这似乎很愚蠢而过多。

我确实注意到我可以致电apiclient.http.HttpRequests,但是我也找不到通过API键通过它的方法。有new_batch_http_request,但我无法从文档中辨别如何将URL传递给它。

谢谢!

得到它!

您不能仅使用目录API查询的密钥,您需要一个服务帐户。

我正在使用google-auth(请参阅此处),因为oauth2client已弃用。

您还需要:

  • 将您的服务帐户的必要权限委托(我的角色具有Viewer的作用,并且可以访问https://www.googleapis.com/auth/auth/admin.directory.device.device.chromeos.chromeos.chromeos.redonly)

  • 在管理员控制台(安全 - >高级设置 - authentication)中分别对其访问它的API访问

  • 获取您的json客户端秘密键并将其放置在您的应用程序中(不要将其包括在VC中)

获得这样的凭据:

credentials = service_account.Credentials.from_service_account_file(
    settings.CLIENT_KEY,
    scopes=settings.SCOPES,
    subject=settings.ADMIN_USER)

admin_user是授权域管理的电子邮件地址

然后您发送get请求:

authed_session = AuthorizedSession(credentials)
response = authed_session.get(request_id_url)

此返回请求您可以通过response.content读取对象。

希望它能帮助别人!

最新更新