如何导出15000多个Google Group成员?

时间:2013-09-20 07:09:45

标签: google-groups google-groups-api google-groups-settings

我的Google网上有大约15000多名会员,我想将它们导出为CSV文件。谷歌集团表示,出口太多有没有其他方法可以出口所有15000多名会员?

2 个答案:

答案 0 :(得分:1)

使用Directory API可以实现这一点。您可以在Google Developer's Console中查看如何使用适当的范围here设置项目。我在下面的代码示例中显示了我是如何为我的电子邮件列表管理器(用Python3编写)做的。请注意,我只是输出到列表,但您也可以写入csv。

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"])

您需要定义一种方法来检索oauth2身份验证令牌(上面代码段中的authToken)。我这样做,但是有专用的库(我不知道的Python3):

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']

您可以从Google开发人员的控制台获取适用于您项目的client_id,client_secret和refresh_token。

答案 1 :(得分:0)

您可以在Google电子表格中导出Google群组中的所有联系人

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

您可以创建触发器以动态添加新联系人,并将您的Google电子表格导出到cvs的文件中。

您可以快速而简单地创建此解决方案。