Skip to content

Instantly share code, notes, and snippets.

@ykhrustalev
Last active August 11, 2020 02:34
Show Gist options
  • Save ykhrustalev/dfedbeb28a7dba641ea2b7c9bfbaa56f to your computer and use it in GitHub Desktop.
Save ykhrustalev/dfedbeb28a7dba641ea2b7c9bfbaa56f to your computer and use it in GitHub Desktop.
#
# pipenv:
#
# [packages]
# google-api-python-client = "*"
# google-auth = "*"
from google.oauth2 import service_account
from googleapiclient.discovery import build
secret_file = 'service-account-credentials.json'
scopes = [
"https://www.googleapis.com/auth/gmail.settings.basic",
]
client_email = "regular-user@domain.com"
# impersonate service accont to be a regular user
credentials = service_account.Credentials.from_service_account_file(
secret_file,
scopes=scopes,
subject=client_email,
)
service = build('gmail', 'v1', credentials=credentials)
r = service.users().settings().sendAs().patch(
userId='me', # referres to the credentials user which is regular user
sendAsEmail=client_email,
body={"signature":"I can't believe it worked"},
).execute()
print(r)
#> {'sendAsEmail': 'regular-user@domain.com', 'displayName': '', 'replyToAddress': '', 'signature': 'I can't believe it worked', 'isPrimary': True, 'isDefault': True}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment