python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

right authentication for desktop application? Authenticate once

Open magowiz opened this issue 3 years ago • 2 comments

I'm trying to use your library as a OneDrive client, I read the authentication part of documentation and I was able to setup a method that works and saves token, the issue is that if I open the app in 60 minutes I can go on, the app recognize the token and I don't need to perform again authentication, if I access after token has expired, I need to perform the authentication again. I would like to know if there is a way to authenticate once, store credentials, and re-generate a token every time is needed, without involving authentication flow, maybe I'm using the wrong method, I described you mine use case to better understand what I need.

Here it is the code I wrote for authentication or token use, (I don't know how to refresh an expired token, but maybe it's impossible):

from O365 import Account, FileSystemTokenBackend
from O365.drive import Folder, Drive, File

SCOPES = ['Files.ReadWrite.All', 'User.Read']

class OneDrive:
    def __init__(self):
        self.credentials = (CLIENT_ID, CLIENT_SECRET)
        self.token_file = 'secrets/onedrive_token.txt'
        token_backend = FileSystemTokenBackend(token_path='secrets',
                                               token_filename=self.token_file)
        self.account = Account(credentials=self.credentials,
                               token_backend=token_backend)
        if not self.account.is_authenticated:
            ret = self.account.authenticate(scopes=SCOPES)
            if ret:
                print('authenticated')
        self.signed = True

magowiz avatar Aug 11 '22 10:08 magowiz

Add offine_access scope

alejcas avatar Aug 12 '22 19:08 alejcas

@janscas Thank you very much, I tried to add that scope yesterday, on first time I had to authenticate since the previous token was generated without it, and today, I was able to login again without authentication,

magowiz avatar Aug 20 '22 12:08 magowiz