3

Admin ADK Directory API ドキュメントの関連ページをすべて読み、stackoverflow に関するいくつかの質問を読みましたが、まだ行き詰っています。

私は Google Apps ドメインの特権管理者であり、ドメイン内のユーザーが独自の Google グループを作成できるようにしたいと考えています。ユーザーがグループの名前とメールアドレスを指定する Google フォームを作成しました。次に、Googleフォームの応答シートには、コードを呼び出してグループを作成する「フォーム送信時」トリガーがあります。

このコードcreateGroupTest()は、スクリプト エディターから実行すると機能します。Google アプリ ドメインにグループがすぐに作成されます。

「フォーム送信時」トリガーが関数を実行する場合、このコードは機能しません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'};
}
4

1 に答える 1

4

I figured it out: I made a mistake in my code, where I didn't include the domain extension after the groupEmail string (because the Google Form only asks the user to fill in the group email name without the domain extension).

I figured I should just delete this question from stackoverflow, but I'll leave it here to document my working oauth authentication code, because:

  1. Something like this could have helped me when I was figuring it out.

  2. There aren't that many posts related to the Google Apps Admin SDK Directory API on here.

  3. There is no support from Google for development-related issues like this, they just direct users here.
于 2014-06-30T12:27:40.907 に答える