1

I am trying to write a Python application which simply adds a user as a delegate to another users mailbox.

I am following the API @ Google API Documentation - Users.settings.delegates: create

However, I am struggling to find how to the parameters of:

User - an account which is TOBE added to a delegate Mailbox Mailbox - the account which has the Mailbox I wish the account to become a delegate of.

I have currently tried making an API which has the delegate user. However, it does not seem to be interacting how I would expect. I am hoping Google will create a responsive API for the browser to support this. However, I am struggling with the code:

from googleapiclient import discovery
from oauth2client.service_account import ServiceAccountCredentials


def main(user_to_be_added, delegated_mailbox):
    service_account_credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials/service_account.json')
    service_account_credentials = service_account_credentials.create_scoped('https://mail.google.com/ https://www.googleapis.com/auth/gmail.insert https://www.googleapis.com/auth/gmail.modify')
    service_account_credentials = service_account_credentials.create_delegated(user_to_be_added)

    service = discovery.build('gmail', 'v1', credentials=service_account_credentials)

    response = service.users().settings().delegates().create().execute(userId=delegated_mailbox)


if __name__ == '__main__':
    main('some_account_to_be_added@gmail.com', 'delegated_mailbox@gmail.com')

Am I interacting with this API completely wrong? If so, how has anyone else achieved this?

Thank you for your time. Jordan

Py.Jordan
  • 415
  • 1
  • 4
  • 16
  • 1
    Don't put an answer if the code is not correct. Edit your original post with your try and delete the new answer. What is the error you are getting? – Jescanellas Sep 24 '19 at 11:59
  • Hey there Jescanellas. Thank you for the tip for SA. I solved the problem below! – Py.Jordan Sep 24 '19 at 14:37

1 Answers1

0

Working Solution:

from googleapiclient import discovery
from google.oauth2 import service_account


def _create_client(subject):
    credentials = service_account.Credentials
    credentials = credentials.from_service_account_file('credentials/service_account.json',
        scopes=['https://www.googleapis.com/auth/gmail.settings.sharing',
                'https://www.googleapis.com/auth/gmail.settings.basic'],
        subject=subject)
    service = discovery.build('gmail', 'v1', credentials=credentials)
    return service


def add_delegate_to_email(user_to_be_added, delegated_mailbox):
    service = _create_client(user_to_be_added)

    body = {
        "delegateEmail": delegated_mailbox,
        "verificationStatus": "accepted"
    }
    try:

        response = service.users().settings().delegates().create(userId='me', body=body).execute()
        print(response)

    except Exception as e:
        print('Exception: {}'.format(e))

Main problem: from oauth2client.service_account import ServiceAccountCredentials is deprecated as Google took ownership with google-auth.

Py.Jordan
  • 415
  • 1
  • 4
  • 16