为开发人员食用API工作空间
我正在尝试消费Google的工作区API,但是我在文档中遇到了极大的困难来创建我的第一个代码,遵循我执行以下
的第一步,
- 我在Google Cloud中创建了一个项目
- ,我启用了Admin SDK API
我创建了一个服务帐户
我创建了JSON格式的密钥
,我添加了唯一的ID和以下范围
[ 'https://www.googleapis.com/auth/apps.order', 'https://www.googleapis.com/auth/siteverification', 'https://www.googleapis.com/auth/directory.readonly', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.reports.usage.usage.readonly', 'https://www.googleapis.com/auth/admin.reports.audit.readonly', 'https://www.googleapis.com/auth/gmail.send' 这是给出的 我想使用链接中的文档 https://developers.google.google。 com/admin-sdk/reports/reference/rether 咨询特定用户的活动,但我找不到使用Python中这些凭据消耗此API的示例代码想要一些帮助。
生成一个令牌,当我尝试使用API时,它没有起作用,并且未经授权,下面是我使用的代码
import requests
url = "https://admin.googleapis.com/admin/reports/v1/activity/users/[email protected]/applications/calendar"
payload = ""
headers = {"Authorization": "Bearer xptoz_exemple_test=PHQbcdddx3xxxxxxxxxxxxddddddddd"}
response = requests.request("GET", url, data=payload, headers=headers)
print(response.text)
I'm trying to consume Google's Workspace APIs, but I'm having extreme difficulty with the documentation to create my first code, following the first steps I did the following
I created a project within Google Cloud
I enabled the Admin SDK API
I created a service account
I created a key in Json format
in the Workspace dashboard under delegation across domain I added the unique id and the following scope
[
'https://www.googleapis.com/auth/apps.order',
'https://www.googleapis.com/auth/siteverification',
'https://www.googleapis.com/auth/directory.readonly',
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.reports.usage.readonly',
'https://www.googleapis.com/auth/admin.reports.audit.readonly',
'https://www.googleapis.com/auth/gmail.send'
]
I would like to use the document from the link https://developers.google.com/admin-sdk/reports/reference/rest to consult the activities of a specific user but I can't find an example code to consume this API using these credentials in Python , I'm new in this area and would like some help.
Generate a token and when I tried to use an api it didn't work and it was unauthorized, below is the code I used
import requests
url = "https://admin.googleapis.com/admin/reports/v1/activity/users/[email protected]/applications/calendar"
payload = ""
headers = {"Authorization": "Bearer xptoz_exemple_test=PHQbcdddx3xxxxxxxxxxxxddddddddd"}
response = requests.request("GET", url, data=payload, headers=headers)
print(response.text)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在未经授权,因为该服务帐户无权完成您要做的事情。要获取许可,您需要使用适当的范围,并且服务帐户不仅需要使用此范围的权限,而且还必须已授予可以访问数据的域上的用户。
首先,您需要确保您的授权代码将其委派给域上的用户。
然后,您应该考虑查看 python quicktart 在此设置以使用安装的应用程序。但是,其余代码应该向您展示如何使用客户端库,而不是像现在一样手动发送所有请求。
You are getting unauthorized because the service account doesn't have permission to do what ever it is you are trying to do. To get permission you need to be using the proper scope, and the service account needs to not only have permission to use this scope but it must have delegated to a user on the domain that has access to the data.
First you need to be sure that your authorization code is delegating to a user on your domain.
Then you should consider having a look at Python quickstart the auth in this is set up to use an installed app. However the rest of the code should show you how to use the Client library rather then sending all the requests manually like you are now.