Why google email auto forwarding throw error when using access_token generated using code?

Viewed 14

Currently Now I am developing code to forward emails onto another email_id.

My code is as below.

flow = flow_from_clientsecrets(settings.CLIENTSECRETS_LOCATION, ' '.join(settings.SCOPES))
flow.redirect_uri = settings.REDIRECT_URI

credentials = flow.step2_exchange(authorization_code)
url = 'https://www.googleapis.com/gmail/v1/users/moon@mydomain.com/settings/forwardingAddresses'
body = {
          "forwardingEmail": "test@mydomain.com",
        }
    
headers = {'Authorization': 'Bearer ' + credentials.access_token}
api_call_response = requests.post(url, headers=headers, json=body)

I have used below scopes

SCOPES = [
    'https://www.googleapis.com/auth/gmail.readonly',
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile',
    'https://www.googleapis.com/auth/gmail.settings.sharing',
    'https://mail.google.com/',
]

I am getting below error:

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

But when I am doing same with postman where I am generating access token using Get New Access Token button api works perfectly.

Postman SS access token: enter image description here

My question is when generating access token from postman It works properly but with code it generating access restriction error.

Thanks.

0 Answers
Related