-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropbox help
51 lines (44 loc) · 1.61 KB
/
Dropbox help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import dropbox
import time
import requests
import base64
import json
# Replace these with your own app credentials
CLIENT_ID = "<app id>"
CLIENT_SECRET = "<app secret>"
refreshtoken = "<refresh token>"
BASIC_AUTH = base64.b64encode(f'{CLIENT_ID}:{CLIENT_SECRET}'.encode())
headers = {
'Authorization': f"Basic {BASIC_AUTH}",
'Content-Type': 'application/x-www-form-urlencoded',
}
data = f'code={refreshtoken}&grant_type=authorization_code'
response = requests.post('https://api.dropboxapi.com/oauth2/token',
data=data,
auth=(CLIENT_ID, CLIENT_SECRET))
# Extract the new access token from the response
access_token = response.json()["access_token"]
refreshtoken = response.json()["refresh_token"]
dbx = dropbox.Dropbox(access_token)
while True:
with open('test.txt', 'rb') as f:
file_contents = f.read()
dbx.files_upload(file_contents, '/test.txt', mode=dropbox.files.WriteMode('overwrite'))
if response.status_code == 401:
refresh_url = "https://api.dropboxapi.com/oauth2/token"
refresh_data = {
"grant_type": "refresh_token",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"refresh_token": refreshtoken,
}
response = requests.post(refresh_url, data=refresh_data)
response.raise_for_status()
# Extract the new access token from the response
access_token = response.json()["access_token"]
refreshtoken = response.json()["refresh_token"]
dbx = dropbox.Dropbox(access_token)
continue
else:
continue
time.sleep(60)