How to get the list of members in a Google group in Google app script (Admin SDK)?

牧云@^-^@ 提交于 2019-11-27 06:29:56

问题


I would like to get a list of members in a Google group using Admin SDK.

But im not getting how to do this. I found below link - https://developers.google.com/admin-sdk/directory/v1/guides/manage-group-members

But I do not know how to use POST method in Google app script.

Can someone please guide me with an example?

UPDATED

I got the output as below, But i would like to access each element (role,email) separately for each member of the group. Is that possible??

{ "role": "OWNER", "kind": "admin#directory#member", "type": "USER", "etag": "\"fdo0/1gUrEe8bli75zvzmqFHyH3cPzlQ\"", "id": "107108832717913338955", "email": "useremailid@domain.com", "status": "ACTIVE" }

Thanks in advance.


回答1:


Please follow the steps,

  1. Login your G drive using super admin account.
  2. Create one Google sheet.
  3. Go to sheet Tool menu -- script Editor.. Save Script project project. Enable admin SDK
  4. Apps script editor click on Resources menu select Advance Google Services -- In Pop up on Admin Directory Apis then Click Google Developers Console link. Developer Console -- click on Library --> search APIs box enter Admin sdk --> click on admin Sdk api link ==> ENABLE and close dev console.

=== At Apps script editor paste following code.

var onSheet = SpreadsheetApp.getActiveSpreadsheet();
var groupKey = "googlegroupid@domainName.com"

function MainGetUserList()
{
    var rows = [];
    var pageToken, page;
    do {
        page = AdminDirectory.Members.list(groupKey,
        {
            domainName: 'YOURDOMAINNAME.@com',
            maxResults: 500,
            pageToken: pageToken,
        });
        var members = page.members
        if (members)
        {
            for (var i = 0; i < members.length; i++)
            {
                var member = members[i];
                var row = [groupKey, member.email, member.role, member.status];
                rows.push(row);
            }
        }
        pageToken = page.nextPageToken;
    } while (pageToken);
    if (rows.length > 1)
    {
        var sheetData = onSheet.getSheetByName("Sheet1")
        var header = ['Group Name', 'User Id', 'User role', 'User Status'];
        sheetData.clear()
        sheetData.appendRow(header).setFrozenRows(1);
        sheetData.getRange(2, 1, rows.length, header.length).setValues(rows);
    }
}

==> Run MainGetUserList() function allow the permission, open you Google Sheet refresh it.

Done.

I have tested this 20k members. Thanks




回答2:


Try to use Members:list to retrive list of all members in a group.

HTTP request

GET https://www.googleapis.com/admin/directory/v1/groups/groupKey/members

Every request your application sends to the Directory API must include an authorization token. The token also identifies your application to Google. Your application must use OAuth 2.0 to authorize requests.

HTTP response:

{
  "kind": "admin#directory#members",
  "etag": etag,
  "members": [
    members Resource
  ],
  "nextPageToken": string
}


来源:https://stackoverflow.com/questions/38784889/how-to-get-the-list-of-members-in-a-google-group-in-google-app-script-admin-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!