O365 / python-o365

A simple python library to interact with Microsoft Graph and Office 365 API
Apache License 2.0
1.6k stars 411 forks source link

I can't use refresh token in airflow environment #1036

Closed SaadAmien20 closed 5 months ago

SaadAmien20 commented 6 months ago

I am building a pipeline to move files from onedrive to azure blob storage through airflow. i got access token in file o365_token.txt after run that code: scopes = ['https://graph.microsoft.com/Files.ReadWrite','https://graph.microsoft.com/offline_access'] credentials = ('app_id','app_secrect') token_backend = FileSystemTokenBackend(token_path='C:\\onedrive-to-azure\\ondrive\\', token_filename='o365_token.txt') account = Account(credentials, tenant_id='tenant_id') if not account.is_authenticated: print ('not authenticate') account.authenticate(scopes=scopes) else: print (' authentcicate') and moved the file to airflow environment and after 60 minutes the token is expired and i got oauthlib.oauth2.rfc6749.errors.TokenExpiredError: (token_expired) During handling of the above exception, another exception occurred: PermissionError: [Errno 13] Permission denied: '/dir/o365_token.txt' My question is how this file work? Hint: I am using airflow after building dockerfile and deploy on Azure kubernetes services.

alejcas commented 5 months ago

try to catch the TokenExpiredError and from there call account.con.refresh_token()

SaadAmien20 commented 5 months ago

thanks for your replay :) I saw the error when 'Airflow' user wants to modify the file using the refresh token, it doesn't have a required permission, I think now the issue is not related to the module. But I've question should I create new token after 14 days ? and how to use the token all time or for example create one every 14 day in non interactive environment ?

alejcas commented 5 months ago

The token is renewed automatically by O365 library: from the readme:

*O365 will automatically refresh the token for you on either authentication method. The refresh token lasts 90 days but it's refreshed on each connection so as long as you connect within 90 days you can have unlimited access.