是否可以通过Google App Maker/Google Apps脚本观看Directory API的更改? [英] Is it possible to watch Directory API changes from Google App Maker/Google Apps Script?

查看:44
本文介绍了是否可以通过Google App Maker/Google Apps脚本观看Directory API的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google App Maker中的性能评估应用程序.当前工具所面临的挑战之一是,当一个人的经理变更或一个人的名字发生变化时,该工具不会与我们的G Suite目录同步-他们现有的评估与该人的旧名字相关联,手动更改.

I'm working on an performance evaluation app in Google App Maker. One of the challenges we have with our current tool is that it doesn't sync with our G Suite directory when a person's manager changes or when a person has a name change -- their existing evaluations are linked to the person's old name and we have to change manually.

在我的新应用程序中,我有一个Employees数据源,其中包括与最初通过Directory API填充的评估本身的关系.在此处阅读文档,看来我应该能够在用户"资源上设置监视,以查找用户更新并对其进行解析,以在员工"数据源中进行适当的名称和管理者更改.不过,我不知道的是监视请求的接收URL.

In my new app, I have an Employees datasource that includes a relation to the evaluation itself that was initially populated via the Directory API. Reading the documentation here, it seems as though I should be able to set up a watch on the Users resource to look for user updates and parse through them to make the appropriate name and manager changes in my Employees datasource. What I can't figure out, though, is what the receiving URL should be for the watch request.

如果有人在Google App Maker中甚至仅在Google Apps脚本中成功完成了此操作,我很想知道您是如何做到的.

If anyone has done this successfully within Google App Maker, or even solely within a Google Apps Script, I'd love to know how you did it.

已编辑添加:

我创建了一个愚蠢的小GAS测试功能,以查看是否可以使用下面的@ dimu-designs解决方案.不幸的是,我刚收到一个Bad Request错误.这就是我所拥有的:

I created a silly little GAS test function to see if I can get @dimu-designs solution below to work. Unfortunately, I just get a Bad Request error. Here's what I have:

function setUserWatch() {
  var optionalArgs = {
    "event": "update"
  };

  var resource = {
    "id": "10ff4786-4363-4681-abc8-28166022425b",
    "type": "web_hook",
    "address": "https://script.google.com/a/.../...hXlw/exec"
  };
  AdminDirectory.Users.watch(resource);
}

地址是当前的Web应用程序URL.

Address is the current web app URL.

已编辑以添加更多内容: 自2014年9月以来,使用GAS接收Web挂钩的(不)功能一直是一个活跃的问题/功能请求- https://issuetracker.google.com/issues/36761910 -一段时间以来,它一直是@ dimu-designs.

EDITED TO ADD MORE: The (in)ability to use GAS to receive web hooks has been an active issue/feature request since Sep 2014 -- https://issuetracker.google.com/issues/36761910 -- which has been @dimu-designs on top of for some time.

推荐答案

这是更全面的答案.

Google支持其许多API的推送通知.但是,它们之间有许多细微的(但不是那么细微的)差异.一些利用Webhook的用户将其数据有效载荷主要作为HTTP标头发送;例如Drive API和Calendar API.其他人则将其有效负载混合在HTTP标头和POST正文中(例如:AdminDirectory API).而且,由于某些API完全利用了不同的机制,它变得更加疯狂(例如:GMail API利用Cloud PubSub).

Google supports push notifications across many of their APIs. However there are many subtle (and not so subtle) differences between them. Some that leverage webhooks send their data payloads primarily as HTTP headers; for example Drive API and Calendar API. Others mix their payloads across HTTP headers and a POST body(ex: AdminDirectory API). And its gets even crazier, with some APIs utilizing different mechanisms altogether (ex: GMail API leverages Cloud PubSub).

每个都有细微差别,但您的目标是在GAS应用程序中利用AdminDirectory推送通知.为此,您需要一个GAS Web应用程序,该应用程序的URL可以用作Web挂钩端点.

There are nuances to each but your goal is to leverage AdminDirectory push notifications in a GAS app. To do that you need a GAS Web App whose URL can serve as a web-hook endpoint.

让我们从以下模板脚本开始,然后从应用程序脚本编辑器"菜单 Publish > Deploy As Web App :

Let's start with the following template script and deploy it as a Web App from the Apps Script Editor menu Publish > Deploy As Web App:

/** HTTP GET request handler */
function doGet(e) {
    return ContentService.createTextOutput("GET message");
}

/** HTTP POST request handler */
function doPost(e) {
    return ContentService.createTextOutput("POST message");
}


步骤2-验证/验证域所有权并添加/注册域

注意: 自2019年8月起,将无法再使用此方法来验证GAS Web应用程序URL. Google Cloud Functions可能是可行的 替代.

部署了Web应用程序之后,您现在必须验证并注册接收URL的域,在这种情况下,该域也是Web应用程序的URL.该网址采用以下格式:

With the web app deployed you now have to verify and register the domain of the receiving url, which in this case is also the web app url. This url takes the following form:

https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/exec

从技术上讲,您不能拥有GAS网络应用网址的域.值得庆幸的是,Google的App Script Gods确实提供了一种机制来验证和注册GAS Web应用程序的网址.

Technically you cannot own a GAS web app url's domain. Thankfully the App Script Gods at Google do provide a mechanism to verify and register a GAS web app url.

从Apps脚本编辑器菜单中选择 Publish > Register in Chrome Web Store .在Chrome网上应用店中注册已发布的网络应用程序还可以验证URL的域(无需在搜索控制台中摆弄).

From the Apps Script Editor menu select Publish > Register in Chrome Web Store. Registering a published web app with the Chrome Web Store also validates the URL's domain (no need to fiddle with the search console).

通过验证后,您需要通过域"验证页面中的域",添加域" API控制台.网址中的所有内容都不含"exec",因此,您将添加一个类似于以下内容的字符串:

Once validated you need to add the "domain" via the Domain verification page in the API Console. The "domain" is everything in the url sans the 'exec', so you'll add a string that looks like this:

https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

对于此步骤,应同时为您的App Script项目和API控制台启用AdminSDK/Directory API服务.

For this step the AdminSDK/Directory API service should be enabled both for your App Script project and in the API Console.

创建一个生成监视请求的函数(可以将其改编为其他事件类型):

Create a function that generates a watch request (this can be retooled for other event types):

function startUpdateWatch() {
    var channel = AdminDirectory.newChannel(),
        receivingURL = "https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/exec",
        gSuiteDomain = "[business-name].com",
        event = "update";

    channel.id = Utilities.getUuid();
    channel.type = "web_hook";
    channel.address = receivingURL + "?domain=" + gSuiteDomain + "&event=" + event;
    channel.expiration = Date.now() + 21600000; // max of 6 hours in the future; Note: watch must be renew before expiration to keep sending notifications

    AdminDirectory.Users.watch(
        channel, 
        {
            "domain":gSuiteDomain,
            "event":event
        }
    );
}

请注意,Directory API推送通知已到期,最长为手表启动后的6个小时,因此必须定期进行更新以确保将通知发送到端点URL.通常,您可以使用基于时间的触发器每隔5个小时左右调用一次此函数.

Note that Directory API push notifications have an expiration, the max being 6 hours from starting the watch so it must be renewed periodically to ensure notifications are sent to the endpoint URL. Typically you can use a time-based trigger to call this function every 5 hours or so.

与其他API的推送机制不同,Directory API会发送POST正文及其通知,因此可以确保在发送通知时触发doPost(e)方法.定制doPost(e)触发器以处理传入事件并重新部署Web应用程序:

Unlike the push mechanisms of other APIs, the Directory API sends a POST body along with its notifications, so the doPost(e) method is guaranteed to be triggered when a notification is sent. Tailor the doPost(e) trigger to handle incoming events and re-deploy the web app:

function doPost(e) {

    switch(e.parameter.event) {
        case "update":
            // do update stuff
            break;

        case "add":
            break;

        case "delete":
            break;
    }

    return ContentService.createTextOutput("POST message");

}

请记住一个警告.有关更新事件的推送通知仅告诉您用户的数据已更新,而不会告诉您更改的确切内容.但这是另一个问题.

There is one caveat to keep in mind. Push notifications for update events only tell you that the user's data was updated, it won't tell you exactly what was changed. But that's a problem for another question.

请注意,我遗漏了很多细节,但这足以使您开始工作.

Note that there are a ton of details I left out but this should be enough to get you up and running.

这篇关于是否可以通过Google App Maker/Google Apps脚本观看Directory API的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆