Dropbox refresh token (#222)

* ask dropbox for refresh token using offline mode
save as json intead of txt file

* need new version of dropbox lib
This commit is contained in:
Rob 2023-06-02 09:13:33 -05:00 committed by GitHub
parent a728596f9a
commit c7685c5508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 15 deletions

View File

@ -5,7 +5,7 @@
<import addon="xbmc.python" version="3.0.0"/> <import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.dateutil" version="2.8.0" /> <import addon="script.module.dateutil" version="2.8.0" />
<import addon="script.module.future" version="0.18.2+matrix.1" /> <import addon="script.module.future" version="0.18.2+matrix.1" />
<import addon="script.module.dropbox" version="9.4.0" /> <import addon="script.module.dropbox" version="10.3.1" />
<import addon="script.module.pyqrcode" version="1.2.1+matrix.1" /> <import addon="script.module.pyqrcode" version="1.2.1+matrix.1" />
</requires> </requires>
<extension point="xbmc.python.script" library="default.py"> <extension point="xbmc.python.script" library="default.py">

View File

@ -1,8 +1,10 @@
import xbmcgui import xbmcgui
import xbmcvfs import xbmcvfs
import json
import pyqrcode import pyqrcode
import resources.lib.tinyurl as tinyurl import resources.lib.tinyurl as tinyurl
import resources.lib.utils as utils import resources.lib.utils as utils
from datetime import datetime
# don't die on import error yet, these might not even get used # don't die on import error yet, these might not even get used
try: try:
@ -37,6 +39,7 @@ class QRCode(xbmcgui.WindowXMLDialog):
class DropboxAuthorizer: class DropboxAuthorizer:
TOKEN_FILE = "tokens.json"
APP_KEY = "" APP_KEY = ""
APP_SECRET = "" APP_SECRET = ""
@ -58,7 +61,7 @@ class DropboxAuthorizer:
def isAuthorized(self): def isAuthorized(self):
user_token = self._getToken() user_token = self._getToken()
return user_token != '' return 'access_token' in user_token
def authorize(self): def authorize(self):
result = True result = True
@ -71,7 +74,7 @@ class DropboxAuthorizer:
self._deleteToken() self._deleteToken()
# copied flow from http://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#dropbox.oauth.DropboxOAuth2FlowNoRedirect # copied flow from http://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#dropbox.oauth.DropboxOAuth2FlowNoRedirect
flow = oauth.DropboxOAuth2FlowNoRedirect(self.APP_KEY, self.APP_SECRET) flow = oauth.DropboxOAuth2FlowNoRedirect(consumer_key=self.APP_KEY, consumer_secret=self.APP_SECRET, token_access_type="offline")
url = flow.start() url = flow.start()
@ -99,7 +102,7 @@ class DropboxAuthorizer:
try: try:
user_token = flow.finish(code) user_token = flow.finish(code)
self._setToken(user_token.access_token) self._setToken(user_token)
except Exception as e: except Exception as e:
utils.log("Error: %s" % (e,)) utils.log("Error: %s" % (e,))
result = False result = False
@ -114,8 +117,8 @@ class DropboxAuthorizer:
if(user_token != ''): if(user_token != ''):
# create the client # create the client
result = dropbox.Dropbox(user_token) result = dropbox.Dropbox(oauth2_access_token=user_token['access_token'], oauth2_refresh_token=user_token['refresh_token'],
oauth2_access_token_expiration=user_token['expiration'], app_key=self.APP_KEY, app_secret=self.APP_SECRET)
try: try:
result.users_get_current_account() result.users_get_current_account()
except: except:
@ -127,21 +130,27 @@ class DropboxAuthorizer:
def _setToken(self, token): def _setToken(self, token):
# write the token files # write the token files
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"), 'w') token_file = open(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE), 'w')
token_file.write(token)
token_file.write(json.dumps({"access_token": token.access_token, "refresh_token": token.refresh_token, "expiration": str(token.expires_at)}))
token_file.close() token_file.close()
def _getToken(self): def _getToken(self):
result = {}
# get token, if it exists # get token, if it exists
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))): if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))):
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt")) token_file = open(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))
token = token_file.read() token = token_file.read()
if(token.strip() != ""):
result = json.loads(token)
# convert expiration back to a datetime object
result['expiration'] = datetime.strptime(result['expiration'], "%Y-%m-%d %H:%M:%S.%f")
token_file.close() token_file.close()
return token return result
else:
return ""
def _deleteToken(self): def _deleteToken(self):
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))): if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))):
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt")) xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))