pythongoogle-apigoogle-workspacegoogle-api-python-clientgoogle-admin-settings-api

Why am I getting error for access restriction when performing code for forwarding emails?


I am new to Google API and currently working on adding forwarding email address using code. The code is as below.

credentials = get_credentials(request.data['code'], request.data['state'])
service = build_gmail_service(credentials)
address = {'forwardingEmail': 'abcd1234@mydomain.com'}
result = service.users().settings().forwardingAddresses().create(userId='me', body=address).execute()

I am able to build google service using the above code but when executing the last line which setting forwarding email address I am getting below error.

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/settings/forwardingAddresses?alt=json returned "Access restricted to service accounts that have been delegated domain-wide authority">

I have searching for a solution for a day but not able to solve it so please let me know if any other have faced same issue and solved it.

Gsuite domain wise delegation image: enter image description here

Thanks.


Solution

  • Access restricted to service accounts that have been delegated domain-wide authority

    Its telling you that you are trying to connect to Admin settings api using a service account but have not properly set up domain wide deligation of the service account from gsuite.

    Your gsuite admin will need to set up the delegation to your service account before you will be allowed to use it.

    enter image description here

    Delegating domain-wide authority to the service account