Skip to content

Commit 583d034

Browse files
committed
Update to Oauth2
1 parent e6bc4fb commit 583d034

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
DROPBOX_TOKEN=YOUR_TOKEN
1+
DROPBOX_APP_KEY=
2+
DROPBOX_APP_SECRET=

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
files/*
3-
.idea/
3+
.idea/
4+
dropbox_token.json

dropbox_login.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json
2+
from datetime import datetime
3+
4+
from dropbox import DropboxOAuth2FlowNoRedirect
5+
6+
'''
7+
It goes through an example of requesting a starting scope,
8+
and requesting more throughout the process
9+
'''
10+
from dotenv import load_dotenv, dotenv_values
11+
12+
load_dotenv()
13+
config = dotenv_values(".env")
14+
15+
DROPBOX_APP_KEY = config["DROPBOX_APP_KEY"]
16+
DROPBOX_APP_SECRET = config["DROPBOX_APP_SECRET"]
17+
18+
auth_flow = DropboxOAuth2FlowNoRedirect(DROPBOX_APP_KEY,
19+
consumer_secret=DROPBOX_APP_SECRET,
20+
token_access_type='offline',
21+
scope=['files.metadata.read', 'files.metadata.write', 'files.content.read',
22+
'files.content.write'])
23+
24+
authorize_url = auth_flow.start()
25+
print("1. Go to: " + authorize_url)
26+
print("2. Click \"Allow\" (you might have to log in first).")
27+
print("3. Copy the authorization code.")
28+
auth_code = input("Enter the authorization code here: ").strip()
29+
30+
try:
31+
oauth_result = auth_flow.finish(auth_code)
32+
# oauth_result = auth_flow.finish(auth_code)
33+
print(oauth_result)
34+
# save to json file
35+
with open('dropbox_token.json', 'w') as f:
36+
37+
output = {
38+
'oauth2_access_token_expiration': datetime.timestamp(oauth_result.expires_at),
39+
'oauth2_refresh_token': oauth_result.refresh_token,
40+
'scope': oauth_result.scope.split(' '),
41+
'oauth2_access_token': oauth_result.access_token,
42+
'app_key': DROPBOX_APP_KEY,
43+
'app_secret': DROPBOX_APP_SECRET
44+
45+
}
46+
json.dump(output, f, indent=4, sort_keys=True, default=str)
47+
print("Token saved to dropbox_token.json")
48+
print(output)
49+
50+
except Exception as e:
51+
print('Error: %s' % (e,))
52+
exit(1)

dropbox_upload.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import json
12
import os
23
import sys
4+
from datetime import datetime
35
from os import listdir
46
from os.path import join
57

68
import dropbox
7-
from dropbox.exceptions import ApiError, AuthError
8-
from dropbox.files import WriteMode
9-
109
from dotenv import load_dotenv, dotenv_values
10+
from dropbox.exceptions import ApiError
11+
from dropbox.files import WriteMode
1112

1213
load_dotenv()
1314
config = dotenv_values(".env")
14-
DROPBOX_TOKEN = config["DROPBOX_TOKEN"]
1515

1616

1717
# Uploads contents of LOCALFILE to Dropbox
@@ -44,25 +44,13 @@ def backup(folder, filename, delete_file=False):
4444

4545

4646
if __name__ == '__main__':
47-
# Check for an access token
48-
if len(DROPBOX_TOKEN) == 0:
49-
sys.exit(
50-
"ERROR: Looks like you didn't add your access token. "
51-
'Open up backup-and-restore-example.py in a text editor and '
52-
'paste in your token in line 14.'
53-
)
5447

55-
# Create an instance of a Dropbox class, which can make requests to the API.
56-
print('Creating a Dropbox object...')
57-
with dropbox.Dropbox(DROPBOX_TOKEN) as dbx:
48+
with open('dropbox_token.json', 'r') as f:
49+
data = json.load(f)
5850

59-
# Check that the access token is valid
60-
try:
61-
dbx.users_get_current_account()
62-
except AuthError:
63-
sys.exit(
64-
'ERROR: Invalid access token; try re-generating an ' 'access token from the app console on the web.'
65-
)
51+
data['oauth2_access_token_expiration'] = datetime.fromtimestamp(data['oauth2_access_token_expiration'])
52+
53+
with dropbox.Dropbox(**data) as dbx:
6654

6755
# Create a backup of the current settings file
6856
folder = 'files'

0 commit comments

Comments
 (0)