I've been trying to get this to work for awhile and I can't get passed this error code I'm getting.
My goal with this code is to set a forwarding email address in gmail. Here is the Google documentation: https://developers.google.com/gmail/api/guides/forwarding_settings
I've copied and pasted the code and I get the same error message:
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/settings/forwardingAddresses?alt=json returned "Bad Request">
Bad request is by far the most frustrating error code to get in return. I'm using a service account with domain-wide delegation so I don't think it's a permissions issue. I've copied the code so it's hard to believe the json package is incorrect. I've looked all over the internet and cannot find example code of anyone actually using this feature. I'm afraid GAM will be my only option.
def test():
key_path = 'tokens/admin_client.json'
API_scopes =['https://www.googleapis.com/auth/gmail.settings.sharing','https://www.googleapis.com/auth/gmail.settings.basic']
credentials = service_account.Credentials.from_service_account_file(key_path,scopes=API_scopes)
gmail_service = build('gmail', 'v1', credentials=credentials)
address = { 'forwardingEmail': '[email protected]' }
gmail_service.users().settings().forwardingAddresses().create(userId='me', body=address).execute()
Please try this code I made. It is working for me:
from googleapiclient import discovery, errors
from oauth2client import file, client, tools
from google.oauth2 import service_account
SERVICE_ACCOUNT_FILE = 'service_account.json'
SCOPES = ['https://www.googleapis.com/auth/gmail.settings.sharing']
# The user we want to "impersonate"
USER_EMAIL = "user@domain"
ADDRESS = { 'forwardingEmail': 'user2@domain' }
# Set the crendentials
credentials = service_account.Credentials.\
from_service_account_file(SERVICE_ACCOUNT_FILE, scopes= SCOPES)
# Delegate the credentials to the user you want to impersonate
delegated_credentials = credentials.with_subject(USER_EMAIL)
try:
# Build the Gmail Service
service = discovery.build('gmail', 'v1', credentials=delegated_credentials)
# Create the forwardingAddresses:Create endpoint
result = service.users().settings().forwardingAddresses().\
create(userId='me', body=ADDRESS).execute()
if result.get('verificationStatus') == 'accepted':
body = {
'emailAddress': result.get('forwardingEmail'),
'enabled': True,
'disposition': 'trash'
}
# If accepted, update the auto forwarding
result_update = service.users().settings().\
updateAutoForwarding(userId='me', body=body).execute()
print(result_update)
# Handle errors if there are
except errors.HttpError as err:
print('\n---------------You have the following error-------------')
print(err)
print('---------------You have the following error-------------\n')
I based the code I'm providing you from Managing Forwarding (the same link you shared), you can also check the Users.settings.forwardingAddresses and the Python API library for more information about the Gmail API library and the endpoints contained there.
You mentioned in one of your comments from another answer that when using .with_subject(arg)
you're getting the error "the user did not have permission". Check OAuth: Managing API client access, it will provide you with some steps to enable the authorizations needed for the occasions when you are using a service account and G Suite.