Import Emails From Google Workspace
01 Mar 2025 - sj, tags: archiving, insights, news, product
Install the google api python client
pip install google-api-python-client
Enable Gmail API and Admin SDK API:
Go to Google Cloud Console
Create a project, eg. “import-emails”
Click on “APIs & Services”, then “Enable APIs and Services”
Enable the “Gmail API” and “Admin SDK API”
Create a Service Account
Click on “APIs & Services”, then “Create credentials”, and select “Service account”
After creating it, click on the service account.
Select the “Keys” tab, click on “Add key” / “Create new key”. The key type shall be “JSON”, then save the json file.
Grant domain wide delegation
We are going to impersonate users. To do that go Google Admin Console
Click on “Security”, then “Access and data control” and “API Controls”.
Then select “Manage Domain Wide Delegation”
Add the Service Account Client ID and authorize the following scopes:
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/admin.directory.user.readonly
Download all emails
Use your Google Workspace admin email (eg. admin@example.com) with the json credentials file you created above:
download-emails-google-workspace.py --admin admin@example.com --credentials import-emails-d25d9743f508.json
The script downloads emails to “emails/user@example.com” directory.
Then all you need to do is to import them:
chown -R piler:piler .
pilerimport -r -d emails/