我已经阅读了Admin ADK Directory API documentation中的所有相关页面以及有关stackoverflow的几个问题,我仍然被卡住了 .
我正在尝试使用Google Apps脚本(Google表格的脚本编辑器中的容器绑定)来创建一个组 . 我是Google Apps域的超级管理员,脚本将像我一样运行 .
这是我到目前为止在脚本编辑器中所做的:
-
去资源 - 高级Google服务...... - 打开了Admin Directory API
-
单击下面的链接以获取Google Developers Console并启用Admin SDK
-
使用了我用来设置用户电子邮件签名的工作代码(改编自this blog post,并将其修改为创建组代码:
function createGroupTest() {
var t = new Date();
t = t.getTime();
createGroup("AAA Test Group " + t, "aaa.testgroup." + t + "@mydomain.com" , "test@mydomain.com", "test");
}
function createGroup(groupName,groupEmail,owner,description) {
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=requestBody;
var url = 'https://www.googleapis.com/admin/directory/v1/groups';
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'};
}
当我运行它时,我收到了这样的回复:
Request failed for returned code 403. Truncated server response: { "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthentica... (use muteHttpExceptions option to examine full response) (line 60, file "Main")
当我添加 fetchArgs.muteHttpExceptions=true;
时,错误输出更改为 Failed to authenticate for service: Groups
.
1 回答
弄清楚了:
去资源 - 高级Google服务......
点击了Google Developers Console的链接
单击侧栏中的“凭据”部分
单击“在公共API访问下创建新密钥”
单击浏览器密钥
添加"?key=",然后将其生成的密钥添加到url字符串的末尾
所以完整的url字符串看起来像这样: