3

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.

Moon
  • 4,014
  • 3
  • 30
  • 66
  • By searching on google I found that you need to allow access on your google account first – Salman B Jul 01 '20 at 09:17
  • yes I have allowed access and then after I have got code and state & by using code and state I have build google service. – Moon Jul 01 '20 at 09:51

1 Answers1

1

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449