簡體   English   中英

在 Google Directory API 中,如何使用 javascript 添加用戶?

[英]In Google Directory API, how do I add a user using javascript?

我有 Google Directory API Javascript quickstart工作。 這部分代碼列出了目錄中的前 10 個用戶:

 gapi.client.directory.users.list({ 'customer': 'my_customer', 'maxResults': 10, 'orderBy': 'email' }).then(function(response) { var users = response.result.users; appendPre('Users:'); appendPre('test') if (users && users.length > 0) { for (i = 0; i < users.length; i++) { var user = users[i]; appendPre('-' + user.primaryEmail + ' (' + user.name.fullName + ')'); } } else { appendPre('No users found.'); } });

我想將用戶添加到目錄。 看起來這是使用users: insert完成的。 因此,從范圍中刪除“只讀”部分后,我將上面的代碼片段替換為:

 var user = { "password": "Testpass123", "primaryEmail": "albert.smith@mydomain.com", "name": { "givenName": "albert", "familyName": "smith" } }; gapi.client.directory.users.insert(user);

顯然這不起作用,但我不確定我錯過了什么。 users:insert 參考頁面上有一個“Try this API”工具,當我在“請求正文”字段中插入“用戶”的屬性時,它會添加用戶。

我不確定如何制作請求正文,並且在文檔中找不到解決方案。 users:list 方法不需要請求正文。 我嘗試了這樣的方法,但也沒有用:

 gapi.client.request({ 'path': 'https://www.googleapis.com/admin/directory/v1/users', 'method': 'POST', 'body': user });

希望有人能給我至少一個大致的想法。 我對此很陌生。

嘗試使用從Apps Script的Admin SDK添加的偽數據添加用戶並用正確的詳細信息替換:

樣品申請機構:

var user = {
    primaryEmail: 'liz@example.com',
    name: {
      givenName: 'Elizabeth',
      familyName: 'Smith'
    },
    // Generate a random password string.
    password: Math.random().toString(36)
  };

嘗試將用戶對象包裝在資源對象中,例如:

var user = {
  resource: {
   "password": "Testpass123",
   "primaryEmail": "albert.smith@mydomain.com",
   "name": {
      "givenName": "albert",
      "familyName": "smith"
    }
  }
}

我再也找不到相關的參考資料,因此也許有人可以發表,但對我有用。

基於https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/insert

function execute() {
            return gapi.client.directory.users.insert({
                "resource": {
                "name": {
                "familyName": "Joey",
                "givenName": "Shmoger"
                },
                "password": "ShmoeyJoey!",
                "primaryEmail": "shmogerjoe@grower.com"
            }
            })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM