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'};
}