Skip to content

Commit

Permalink
Add support for token.json file
Browse files Browse the repository at this point in the history
- Before this change, DRIVE_TOKEN env variable was only way for authorization
- Now adding token.json file to repo will also work
- TOKEN_JSON_URL env variable can be used for authorization too
- Change DRIVE_TOKEN env variable from Required Config to Optional Config
  • Loading branch information
l3v11 authored Mar 17, 2022
1 parent 5843d4d commit 616a4fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 20 additions & 6 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ def get_config(name: str):
except:
pass

try:
TOKEN_JSON_URL = get_config('TOKEN_JSON_URL')
if len(TOKEN_JSON_URL) == 0:
TOKEN_JSON_URL = None
else:
res = requests.get(TOKEN_JSON_URL)
if res.status_code == 200:
with open('token.json', 'wb+') as f:
f.write(res.content)
f.close()
else:
LOGGER.error(f"Failed to load token.json file [{res.status_code}]")
raise KeyError
except KeyError:
pass

try:
BOT_TOKEN = get_config('BOT_TOKEN')
OWNER_ID = int(get_config('OWNER_ID'))
Expand All @@ -75,13 +91,11 @@ def get_config(name: str):
exit(1)

try:
if len(get_config('DRIVE_TOKEN')) == 0 or str(get_config('DRIVE_TOKEN')).lower() == "empty":
LOGGER.error("DRIVE_TOKEN var is missing")
exit(1)
with open('token.json', 'wt') as f:
f.write(get_config('DRIVE_TOKEN').replace("\n",""))
if not os.path.exists('token.json'):
with open('token.json', 'wt') as f:
f.write(get_config('DRIVE_TOKEN').replace("\n", ""))
except:
LOGGER.error("Failed to create token.json file")
LOGGER.error("token.json file is missing")
exit(1)

try:
Expand Down
3 changes: 2 additions & 1 deletion config_sample.env
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# REQUIRED CONFIG
BOT_TOKEN=
OWNER_ID=
DRIVE_TOKEN=
DRIVE_FOLDER_ID=
TELEGRAPH_ACCS=
# OPTIONAL CONFIG
DRIVE_TOKEN=
AUTHORIZED_CHATS=
DATABASE_URL=
IS_TEAM_DRIVE=
USE_SERVICE_ACCOUNTS=
DRIVE_INDEX_URL=
TOKEN_JSON_URL=
ACCOUNTS_ZIP_URL=
DRIVE_LIST_URL=
APPDRIVE_EMAIL=
Expand Down

0 comments on commit 616a4fe

Please sign in to comment.