【问题标题】:POST request for Google Admin SDK in Javascript/Jquery在 Javascript/Jquery 中对 Google Admin SDK 的 POST 请求
【发布时间】:2016-05-23 10:15:56
【问题描述】:

尝试使用对目录的发布请求(Admin SDK)向用户添加别名。我知道 JS 客户端库仍处于测试阶段,但它是我要做的最简单的解决方案。

以下返回 401 错误。我的凭据在控制台上很好。

    <script>
  var apiKey = "my_key";
  var clientId = "my_client_id";
  var scopes = "https://www.googleapis.com/auth/admin.directory.user.alias";
  var users = {}; // external JSON file

  function load() {
    gapi.client.setApiKey(apiKey);
    gapi.auth.authorize({
      client_id: clientId,
      scope: scopes,
      immediate: true
    },

    function(){
      for (var i = 0; i < users.length; i++) {
        var thisUser = users[i]["emailAddress"];
        var thisAlias = users[i]["secondaryEmail"];
        var thisURL = "https://www.googleapis.com/admin/directory/v1/users/" + thisUser + "/aliases"

        $.post(thisURL, {alias: thisAlias});
      }
    });
  }
</script>
<script src="https://apis.google.com/js/client.js?onload=load"></script>

【问题讨论】:

    标签: javascript jquery post google-admin-sdk google-directory-api


    【解决方案1】:

    您可以尝试按照JavaScript Quickstart中的示例代码:

    // Your Client ID can be retrieved from your project in the Google
    // Developer Console, https://console.developers.google.com
    var CLIENT_ID = '<YOUR_CLIENT_ID>';
    
    var SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly'];
    
    /**
    * Check if current user has authorized this application.
    */
    function checkAuth() {
    gapi.auth.authorize(
    {
    'client_id': CLIENT_ID,
    'scope': SCOPES.join(' '),
    'immediate': true
    }, handleAuthResult);
    }
    
    /**
    * Handle response from authorization server.
    *
    * @param {Object} authResult Authorization result.
    */
    function handleAuthResult(authResult) {
    var authorizeDiv = document.getElementById('authorize-div');
    if (authResult && !authResult.error) {
    // Hide auth UI, then load client library.
    authorizeDiv.style.display = 'none';
    loadDirectoryApi();
    } else {
    // Show auth UI, allowing the user to initiate authorization by
    // clicking authorize button.
    authorizeDiv.style.display = 'inline';
    }
    }
    

    尝试将您的代码格式更改为此,我认为这会奏效。

    【讨论】:

    • 请原谅迟到的回复,感谢您的发帖。这或多或少奏效了。我还有其他不相关的并发症,但这回答了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2013-10-07
    • 2014-10-08
    • 2014-11-06
    • 2010-10-16
    • 2017-09-06
    相关资源
    最近更新 更多