Authenticate Gmail delegated account with OAuth in python

0

I'm trying to get labels from a delegated Gmail account (a mailbox for the job). The problem is that I always get an error of the delegated account. To prove it I have created two accounts:

[email protected] [email protected]

I have set up the account [email protected] to access from [email protected]

I have entered the google console activating the use of the Gmail API. I have extracted the client_secret from both accounts to test. I have been launching the following script with different results according to userId.

from __future__ import print_function from googleapiclient.discovery import build from httplib2 import Http from oauth2client import file, client, tools

# If modifying these scopes, delete the file token.json. SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

def main():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        #flow = client.flow_from_clientsecrets('credentials.json.txt', SCOPES)
        flow = client.flow_from_clientsecrets('client_secret_778365488036-aiik518cf5ohcvd71hmrpvuhk9ovg6o6.apps.googleusercontent.com.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    # Call the Gmail API
    results = service.users().labels().list(userId='me').execute()
    labels = results.get('labels', [])

    if not labels:
        print('No labels found.')
    else:
        print('Labels:')
        for label in labels:
            print(label['name'])

if __name__ == '__main__':
    main()

The files that go client.flow_from_clientsecrets, are those of both accounts. When I tried to use the clientSecret of the mailbox + authenticate with my account felipe.rodriguez.fonte.dev, I get the labels of the dev account, but I need to access the mailbox with the credentials of dev (which is the case that I have at work).

When I authenticate with clientSecret of the DEV account, and in UserId I put the address of the mailbox, it gives me a 403 for the delegation.

Does anyone have any idea what I'm doing wrong?

    
asked by Felipe Rodriguez Fonte 26.12.2018 в 11:54
source

0 answers