Search code examples
pythongrapho365-flow

multiuser management in O365 python


I was creating an application of office which contain multiple user credentials and do the tasks like emailing and adding calender events. I choosed O365. All things were great here except. I could not save the credentials. like in other google products we pickle the creds.

with open(f'account_data/{account_name}.pickle','wb') as stream:
    pickle.dump(account, stream)

but I error as

AttributeError: Can't pickle local object 'OAuth2Session.__init__.<locals>.<lambda>'

I need to store multiple user keys and do some tasks. If you have any other module then tell me.


Solution

  • I figured it out myself.

    from O365 import Account, MSGraphProtocol, message, FileSystemTokenBackend
    def new_account(account_name):
        account = Account(credentials, scopes=scopes, )
        token_backend = FileSystemTokenBackend(token_path='account_data', token_filename=f'{account_name}.txt')
        account.con.token_backend = token_backend
        account.authenticate()
        account.con.token_backend.save_token()
    
    def load_account(account_name):
        account = Account(credentials, scopes=scopes, )
        token_backend = FileSystemTokenBackend(token_path='account_data', token_filename=f'{account_name}.txt')
        account.con.token_backend = token_backend
        account.con.token_backend.load_token()
        if account.con.refresh_token():
            return account