实施 Google 目录 API 用户使用 Python 观看

Implementing Google Directory API users watch with Python

我在理解和实施 Google 目录 API 的用户监视功能和推送通知系统 (https://developers.google.com/admin-sdk/reports/v1/guides/push#creating-notification-channels) 时遇到了一些困难 Python GAE应用程序。我想要实现的是任何使用我的应用程序的用户(管理员)都能够在他自己的域内看到用户更改。

我已经验证了要用于通知的域并按如下方式实现了监视请求:

directoryauthdecorator = OAuth2Decorator(
approval_prompt='force',
client_id='my_client_id',
client_secret='my_client_secret',
callback_path='/oauth2callback',
scope=['https://www.googleapis.com/auth/admin.directory.user'])

class PushNotifications(webapp.RequestHandler):
      @directoryauthdecorator.oauth_required
      def get(self):
          auth_http = directoryauthdecorator.http()
          service = build("admin", "directory_v1", http=auth_http)

          uu_id=str(uuid.uuid4())
          param={}
          param['customer']='my_customer'
          param['event']='add'
          param['body']={'type':'web_hook','id':uu_id,'address':'https://my-domain.com/pushNotifications'}
          watchUsers = service.users().watch(**param).execute()

application = webapp.WSGIApplication(
                         [
                          ('/pushNotifications',PushNotifications),
                          (directoryauthdecorator.callback_path, directoryauthdecorator.callback_handler())],
                         debug=True)

现在,接收部分是我不明白的。当我在我的域中添加用户并检查应用程序的请求日志时,我看到一些 activity,但没有可用数据。我应该如何处理这部分?

如有任何帮助,我们将不胜感激。谢谢。

问题

处理程序的实现似乎有些混乱。您的处理程序实际上 通过向 Reports API 端点发送 POST 请求来设置通知通道 。正如文档所说:

To set up a notification channel for messages about changes to a particular resource, send a POST request to the watch method for the resource.

source

您只需发送此请求一次即可设置频道,"address" 参数应为您应用程序上将接收通知的 URL 参数。

此外,不清楚以下代码发生了什么:

param={}
param['customer']='my_customer'
param['event']='add'

您只是为了 post 破坏密码吗?或者它实际上是这样写在文件中的吗?您实际上应该尽可能多地保留您的应用程序 运行 的代码,以便我们可以对其进行推理。

解决方法

从您链接的文档看来 - 在“Receiving Notifications”部分,您应该在 "address" 中指定代码以接收将检查 POST 请求的通知body 和 headers 通知推送请求,然后对该数据执行某些操作(例如将其存储在 BigQuery 中或向管理员发送电子邮件等)

设法弄明白了。在 App Engine 日志中,我注意到每次我在我的域上进行更改时,即 'watched',我都会收到来自 Google 的 API 的 POST 请求,但带有 302 代码。我发现这是因为我在我的 app.yaml 中为脚本配置了 login: required,它正在处理请求并且 POST 请求被重定向到登录页面,而不是处理脚本。