在Google Apps脚本中使用Admin SDK Directory API创建一个组 - 错误403 [英] Creating a group with Admin SDK Directory API in Google Apps Script - Error 403

查看:194
本文介绍了在Google Apps脚本中使用Admin SDK Directory API创建一个组 - 错误403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读管理ADK目录​​API文档和几个关于计算器的问题,我仍然坚持。

我尝试使用Google Apps脚本(Google表格的脚本编辑器中的容器绑定)来创建组。我是我的Google Apps网域的超级管理员,脚本将以我的身份运行。



以下是我在脚本编辑器中所做的工作:
$ b


  1. 加入资源 - 高级Google服务... - 启用管理目录API


  2. 点击下面的Google Developers Console链接,并启用管理员SDK


  3. 使用了我用来设置用户电子邮件签名的工作代码(这是从此博文,并对其进行修改以创建组:

     函数createGroupTest(){

    var t = new Date();
    t = t.getTime();

    createGroup(AAA Test Group+ t,aaa.testgroup。+ t +@ mydomain.com,test@mydomain.com,test );

    }

    函数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);



    $ b 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. ,我得到这个回应:

     请求失败,返回的代码为403.截断的服务器响应:{error:{errors :[{domain:usageLimits,reason:dailyLimitExceededUnreg,message:Unauthentica的每日限制...(使用muteHttpExceptions选项检查完整响应)(第60行,文件Main)

    当我添加 fetchArgs.muteHttpExceptions = true; 错误输出更改为无法为服务进行身份验证:组

    解决方案

    发现:


    1. 加入资源 - 高级Google服务...

    2. 点击链接为Google开发者控制台

    3. 点击边栏中的凭证部分

    4. 在公共API访问下点击创建新密钥

    5. 点击浏览器密钥

    6. 添加?key =,后跟密钥它生成到url字符串的结尾

    所以完整的url字符串看起来像这样:

      var url ='https://www.googleapis.com/admin/directory/v1/groups?key=XXXXXXXXXXX-XXXXXXXXXXXXX-XXXXXXXXXXXXX'; 


    I've read through all of the relevant pages in the Admin ADK Directory API documentation and several questions on stackoverflow, and I'm still stuck.

    I'm trying to use Google Apps Script (container-bound within the Script Editor of a Google Sheet) to create a group. I am the super admin of my Google Apps domain, and the scripts will be running as me.

    Here's what I did in the Script Editor so far:

    1. Went to Resources - Advanced Google Services... - turned on the Admin Directory API

    2. Clicked the link below that for the Google Developers Console and enabled the Admin SDK

    3. Took working code that I have which I use to set user's email signatures (which was adapted from this blog post, and modified it for creating groups instead:

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

    When I run that, I get this response:

    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")
    

    When I add fetchArgs.muteHttpExceptions=true; the error output changes to Failed to authenticate for service: Groups.

    解决方案

    Figured it out:

    1. Went to Resources - Advanced Google Services...
    2. Clicked the link for the Google Developers Console
    3. Clicked on the Credentials section in the sidebar
    4. Clicked Create New Key under Public API access
    5. Clicked Browser Key
    6. Added "?key=" followed by the key that it generated to the end of the url string

    So the complete url string looked like this:

    var url = 'https://www.googleapis.com/admin/directory/v1/groups?key=XXXXXXXXXXX-XXXXXXXXXXXXX-XXXXXXXXXXXXX';
    

    这篇关于在Google Apps脚本中使用Admin SDK Directory API创建一个组 - 错误403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆