Skip to content

Commit

Permalink
Dropbox refresh token (#222)
Browse files Browse the repository at this point in the history
* ask dropbox for refresh token using offline mode
save as json intead of txt file

* need new version of dropbox lib
  • Loading branch information
robweber authored Jun 2, 2023
1 parent a728596 commit c7685c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<import addon="xbmc.python" version="3.0.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.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" />
</requires>
<extension point="xbmc.python.script" library="default.py">
Expand Down
37 changes: 23 additions & 14 deletions resources/lib/authorizers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import xbmcgui
import xbmcvfs
import json
import pyqrcode
import resources.lib.tinyurl as tinyurl
import resources.lib.utils as utils
from datetime import datetime

# don't die on import error yet, these might not even get used
try:
Expand Down Expand Up @@ -37,6 +39,7 @@ def onClick(self, controlId):


class DropboxAuthorizer:
TOKEN_FILE = "tokens.json"
APP_KEY = ""
APP_SECRET = ""

Expand All @@ -58,7 +61,7 @@ def setup(self):
def isAuthorized(self):
user_token = self._getToken()

return user_token != ''
return 'access_token' in user_token

def authorize(self):
result = True
Expand All @@ -71,7 +74,7 @@ def authorize(self):
self._deleteToken()

# 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()

Expand Down Expand Up @@ -99,7 +102,7 @@ def authorize(self):

try:
user_token = flow.finish(code)
self._setToken(user_token.access_token)
self._setToken(user_token)
except Exception as e:
utils.log("Error: %s" % (e,))
result = False
Expand All @@ -114,8 +117,8 @@ def getClient(self):

if(user_token != ''):
# 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:
result.users_get_current_account()
except:
Expand All @@ -127,21 +130,27 @@ def getClient(self):

def _setToken(self, token):
# write the token files
token_file = open(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"), 'w')
token_file.write(token)
token_file = open(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE), 'w')

token_file.write(json.dumps({"access_token": token.access_token, "refresh_token": token.refresh_token, "expiration": str(token.expires_at)}))
token_file.close()

def _getToken(self):
result = {}
# get token, if it exists
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))):
token_file = open(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() + self.TOKEN_FILE))
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()

return token
else:
return ""
return result

def _deleteToken(self):
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "tokens.txt"))):
xbmcvfs.delete(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() + self.TOKEN_FILE))

0 comments on commit c7685c5

Please sign in to comment.