简体   繁体   中英

How to export 15000+ Google Group members?

There are around 15000+ members in my Google Group and I'd like to export them to CSV file. Google Groups says there are too many to export Is there any other way to export all the 15000+ members?

This is possible using the Directory API. You can see how to set up a project in the Google Developer's Console with the appropriate scopes here . I have a sample of code below that shows how I did this for my email list manager (written in Python3). Note that mine just outputs to a list, but you could write to a csv as well.

import urllib.request, urllib.parse, urllib.error
import json

req = urllib.request.Request("https://www.googleapis.com/admin/directory/v1/groups/%s/members" % (group.replace("@", "%40").replace(".", "%2E")))
req.add_header("Authorization", "Bearer %s" % (authToken))
rawResponse = urllib.request.urlopen(req)
fResponse = json.loads(rawResponse.readall().decode('utf-8'))
if "members" in fResponse:
    curMembers = fResponse["members"]
    while "nextPageToken" in fResponse:
        req = urllib.request.Request("https://www.googleapis.com/admin/directory/v1/groups/%s/members?pageToken=%s" % (group.replace("@", "%40").replace(".", "%2E"), fResponse['nextPageToken']))
        req.add_header("Authorization", "Bearer %s" % (authToken))
        rawResponse = urllib.request.urlopen(req)
        fResponse = json.loads(rawResponse.readall().decode('utf-8'))
        if "members" in fResponse:
            curMembers.extend(fResponse["members"])

You'll need to define a way to retrieve the oauth2 authentication token (authToken in the above snippet). I do so as below, but there are dedicated libraries for this (just not for Python3 that I know of):

def RenewToken():
    """
    This function retrieves an authentication token from Google which will allow the script
    to view and edit group membership
    """
    clientID = "client_id.apps.googleusercontent.com"
    clientSecret = "client_secret"
    grantType = "refresh_token"
    responseType = "code"
    refreshToken = "refresh_token"
    requestDict = {"client_secret": clientSecret,  #field-value pairs for the request body
                   "grant_type": grantType,
                   "refresh_token": refreshToken,
                   "client_id": clientID}
    requestUri = "https://accounts.google.com/o/oauth2/token"
    requestString = urllib.parse.urlencode(requestDict)
    requestBody = requestString.encode('utf-8')
    request = urllib.request.Request(requestUri, data=requestBody)
    response = urllib.request.urlopen(request)
    responseDict = json.loads(response.readall().decode('utf-8'))
    return responseDict['access_token']

You can get client_id, client_secret and refresh_token from the Google developer's console for your project.

you can export all contacts in your google group in a Google Spreadsheet

link : https://developers.google.com/apps-script/reference/groups/

After you can create a trigger to add new contact dynamically, and export you Google spreadsheet on a cvs's file.

you can create this solution quickly and simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM