繁体   English   中英

在 Google Apps 脚本中使用 Admin SDK Directory API 创建组不起作用“提交表单”

[英]Creating a group with Admin SDK Directory API in Google Apps Script doesn't work "On form submit"

我已经阅读了 Admin ADK Directory API 文档中的所有相关页面以及有关 stackoverflow 的几个问题,但我仍然卡住了。

我是我的 Google Apps 域的超级管理员,我希望我的域中的用户能够创建自己的 Google 网上论坛。 我制作了一个 Google 表单,用户在其中指定了组的名称和电子邮件。 然后 Google Form Responses 表有一个“On form submit”触发器,它调用我的代码来创建组。

当我从脚本编辑器运行createGroupTest()时,此代码有效。 它会立即在我的 Google Apps 域中创建该组。

当“On form submit”触发器运行onFormSubmit(e)函数时,此代码不起作用 我收到来自catch(e)的电子邮件,上面写着Exception: Failed to authenticate for service: Groups

有谁知道是什么导致 oauth 身份验证在脚本编辑器中起作用,但在被 onFormSubmit 函数调用时却不知道?

function onFormSubmitTest() {
 
  var t = new Date();
  t = t.getTime();
  
  onFormSubmit([t, "AAA Test Group " + t], ["aaa.testgroup." + t + "@mydomain.com"], ["me@mydomain.com"]);
  
}

var consumerKey = "mydomain.com";
var consumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxx";
var domainName = "mydomain.com";

function onFormSubmit(e) {
  
  var timestamp  = e.values[0];
  var groupName  = e.values[1];
  var groupEmail = e.values[2];
  var owner      = e.values[3];
  
  owner = owner.split("@")[0];
  
  var description = 'test';
  
  var requestBody = {email: groupEmail, name: groupName, description: description};
             
  var scope = "https://www.googleapis.com/auth/admin.directory.group";
  
  var fetchArgs                = googleOAuth_("Groups", scope);
  fetchArgs.method             = "POST";
  fetchArgs.contentType        = "application/json";
  fetchArgs.payload            = JSON.stringify(requestBody);
  fetchArgs.muteHttpExceptions = true;
   
  var url = 'https://www.googleapis.com/admin/directory/v1/groups?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  
  UrlFetchApp.fetch(url, fetchArgs);

}
 

function googleOAuth_(name,scope) {
  var oAuthConfig = UrlFetchApp.addOAuthService(name)
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey(consumerKey);
  oAuthConfig.setConsumerSecret(consumerSecret);
  return {oAuthServiceName:name, oAuthUseToken:'always'};
}

我想通了:我没有在groupEmail字符串中包含域扩展名(因为我的 Google 表单只要求用户填写没有域扩展名的组电子邮件名称)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM