【问题标题】:Creating a group with Admin SDK Directory API in Google Apps Script doesn't work "On form submit"在 Google Apps 脚本中使用 Admin SDK Directory API 创建组不起作用“提交表单”
【发布时间】:2014-08-20 19:36:53
【问题描述】:

我已经阅读了 Admin ADK Directory API documentation 中的所有相关页面以及有关 stackoverflow 的几个问题,但我仍然陷入困境。

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

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

当“提交表单”触发器运行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'};
}

【问题讨论】:

  • 这段代码还有效吗?这是否只需要开发人员控制台中的密钥还是需要 oauth2?您能否就此提供一些说明?
  • @Riccardo 是的,这在 2021 年仍然有效,并且仍在我的代码中使用。它使用由 Google Apps 脚本团队积极维护的 OAuth2 for Apps Script 库。 googleOAuth_ 取自该库,但您只需要添加库,无需将函数添加到您自己的代码中。

标签: google-apps-script oauth-2.0 google-apps google-admin-sdk google-directory-api


【解决方案1】:

我想通了:我在我的代码中犯了一个错误,我没有在 groupEmail 字符串之后包含域扩展名(因为 Google 表单只要求用户填写组电子邮件名称​​没有 域扩展名)。

我想我应该从 stackoverflow 中删除这个问题,但我将把它留在这里记录我的工作 oauth 身份验证代码,因为:

  1. 当我想办法解决这个问题时,这样的事情可能会对我有所帮助。

  2. 此处与 Google Apps Admin SDK Directory API 相关的帖子并不多。

  3. Google 不支持此类与开发相关的问题,他们只是将用户引导至此处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2018-09-16
    • 2019-03-26
    • 1970-01-01
    相关资源
    最近更新 更多