2

我使用Google Directory API Javascript quickstart工作。在這部分代碼列出前10個用戶在目錄:在Google Directory API中,如何添加使用javascript的用戶?

 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": "[email protected]", 
 
     "name": { 
 
      "givenName": "albert", 
 
      "familyName": "smith" 
 
     } 
 
     }; 
 
     
 
gapi.client.directory.users.insert(user);

顯然,這並不工作,但我不確定我錯過了什麼。在用戶上有一個「試用此API」工具:插入參考頁面,當我在「請求正文」字段中插入'user'的屬性時,它會添加用戶。

雖然我不確定如何創建請求主體,但我無法在文檔中找到解決方案。 users:list方法不需要請求主體。我想這樣的事情,也沒有工作:

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

希望有人能給我至少做什麼總體思路。我很新。

回答

0

嘗試從Apps Script's Admin SDK Add user得出這個虛擬數據,並與您的正確的信息替換:

樣本請求體:

var user = { 
    primaryEmail: '[email protected]', 
    name: { 
     givenName: 'Elizabeth', 
     familyName: 'Smith' 
    }, 
    // Generate a random password string. 
    password: Math.random().toString(36) 
    }; 
+0

這看起來我的要求的身體很相似,我敢肯定,要麼會工作。 我的問題是,我不知道如何處理這個'用戶'變量。我試過'gapi.client.directory.users.insert(用戶);'但那不起作用。 換句話說:我如何在user.insert函數中插入一個請求體? – squarjn

相關問題