在 1 个 URLFetchApp 调用中将几个成员添加到 Google 组?
Adding a few members to a Google Group in 1 URLFetchApp Call?
是否可以使用 1 URLFetchApp.fetch 调用将少量成员添加到 Google 组?尝试减少我的应用程序中的提取调用量。
我可以为每个成员打 1 次电话,将他们添加到 Google 组。
var obj = JSON.parse(jsonStr);
for(var h=1; h< mailingData.length;h++) {
obj["members"].push({"email":mailingData[h][column],"role":"MEMBER"});
}
jsonStr = JSON.stringify(obj);
Logger.log(jsonStr);
fetchArgs.method = "POST";
fetchArgs.contentType = "application/json";
fetchArgs.headers = {Authorization: 'Bearer ' + myService.getAccessToken()};
fetchArgs.muteHttpExceptions=true;
fetchArgs.payload = jsonStr;
var url = 'https://www.googleapis.com/admin/directory/v1/groups/'+group+'/members';
var res = UrlFetchApp.fetch(url, fetchArgs);
这是 jsonStr 的打印输出:
{"members":[
{"email":"devon@myorg.com","role":"MEMBER"},
{"email":"jake_smith@myorg.com","role":"MEMBER"},
{"email":"robert_keys@myorg.com","role":"MEMBER"}
]}
The error I get when I print the response is the following:
[19-07-31 16:21:18:451 EDT] {
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Missing required field: member"
}
],
"code": 400,
"message": "Missing required field: member"
}
}
在应用程序脚本中,您可以使用 fetchAll() 函数进行批量请求 [1]。我在您的代码中实现了 fetchAll,创建了一个空的请求数组,并用每个用户的每个请求填充它。假设您从 mailingData 数组、正确的授权令牌和 url 中的现有组电子邮件中收到一封电子邮件,以下代码将起作用(我使用这些更改对其进行了测试):
var requests = []
for(var h=1; h< mailingData.length; h++) {
var data = {"email":mailingData[h][column], "role":"MEMBER"};
var jsonStr = JSON.stringify(data);
var request = {};
request.url = 'https://www.googleapis.com/admin/directory/v1/groups/'+group+'/members';
request.method = "POST";
request.contentType = "application/json";
request.headers = {Authorization: 'Bearer ' + myService.getAccessToken()};
request.muteHttpExceptions=true;
request.payload = jsonStr;
requests.push(request);
}
var response = UrlFetchApp.fetchAll(requests);
[1] https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchAll(Object)
是否可以使用 1 URLFetchApp.fetch 调用将少量成员添加到 Google 组?尝试减少我的应用程序中的提取调用量。
我可以为每个成员打 1 次电话,将他们添加到 Google 组。
var obj = JSON.parse(jsonStr);
for(var h=1; h< mailingData.length;h++) {
obj["members"].push({"email":mailingData[h][column],"role":"MEMBER"});
}
jsonStr = JSON.stringify(obj);
Logger.log(jsonStr);
fetchArgs.method = "POST";
fetchArgs.contentType = "application/json";
fetchArgs.headers = {Authorization: 'Bearer ' + myService.getAccessToken()};
fetchArgs.muteHttpExceptions=true;
fetchArgs.payload = jsonStr;
var url = 'https://www.googleapis.com/admin/directory/v1/groups/'+group+'/members';
var res = UrlFetchApp.fetch(url, fetchArgs);
这是 jsonStr 的打印输出:
{"members":[
{"email":"devon@myorg.com","role":"MEMBER"},
{"email":"jake_smith@myorg.com","role":"MEMBER"},
{"email":"robert_keys@myorg.com","role":"MEMBER"}
]}
The error I get when I print the response is the following:
[19-07-31 16:21:18:451 EDT] {
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Missing required field: member"
}
],
"code": 400,
"message": "Missing required field: member"
}
}
在应用程序脚本中,您可以使用 fetchAll() 函数进行批量请求 [1]。我在您的代码中实现了 fetchAll,创建了一个空的请求数组,并用每个用户的每个请求填充它。假设您从 mailingData 数组、正确的授权令牌和 url 中的现有组电子邮件中收到一封电子邮件,以下代码将起作用(我使用这些更改对其进行了测试):
var requests = []
for(var h=1; h< mailingData.length; h++) {
var data = {"email":mailingData[h][column], "role":"MEMBER"};
var jsonStr = JSON.stringify(data);
var request = {};
request.url = 'https://www.googleapis.com/admin/directory/v1/groups/'+group+'/members';
request.method = "POST";
request.contentType = "application/json";
request.headers = {Authorization: 'Bearer ' + myService.getAccessToken()};
request.muteHttpExceptions=true;
request.payload = jsonStr;
requests.push(request);
}
var response = UrlFetchApp.fetchAll(requests);
[1] https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchAll(Object)