为开发人员食用API工作空间

发布于 2025-01-22 05:37:42 字数 1451 浏览 4 评论 0原文

我正在尝试消费Google的工作区API,但是我在文档中遇到了极大的困难来创建我的第一个代码,遵循我执行以下

的第一步,

  1. 我在Google Cloud中创建了一个项目

  2. ,我启用了Admin SDK API

  3. 我创建了一个服务帐户

  4. 我创建了JSON格式的密钥

  5. ,我添加了唯一的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

  1. I created a project within Google Cloud

  2. I enabled the Admin SDK API

  3. I created a service account

  4. I created a key in Json format

  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装纯掩盖桑 2025-01-29 05:37:42

您正在未经授权,因为该服务帐户无权完成您要做的事情。要获取许可,您需要使用适当的范围,并且服务帐户不仅需要使用此范围的权限,而且还必须已授予可以访问数据的域上的用户。

首先,您需要确保您的授权代码将其委派给域上的用户。

#!/usr/bin/python

from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build


scopes = [            'https://www.googleapis.com/auth/admin.reports.usage.readonly'        ]

credentials = ServiceAccountCredentials.from_json_keyfile_name('/path/to/my/key.json', scopes)
delegated_credentials = credentials.create_delegated('[email protected]')

http_auth = credentials.authorize(Http())

service = build('admin', 'directory_v1', credentials=creds)

然后,您应该考虑查看 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.

#!/usr/bin/python

from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build


scopes = [            'https://www.googleapis.com/auth/admin.reports.usage.readonly'        ]

credentials = ServiceAccountCredentials.from_json_keyfile_name('/path/to/my/key.json', scopes)
delegated_credentials = credentials.create_delegated('[email protected]')

http_auth = credentials.authorize(Http())

service = build('admin', 'directory_v1', credentials=creds)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文