/
cantrefresh
52 lines (45 loc) · 1.49 KB
/
cantrefresh
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
52
import dropbox
import time
import requests
import base64
import json
APP_KEY = 'APP KEY'
APP_SECRET = 'APP Secret'
REFRESH_TOKEN = 'Refresh Token'
BASIC_AUTH = base64.b64encode(f'{APP_KEY}:{APP_SECRET}'.encode())
headers = {
'Authorization': f"Basic {BASIC_AUTH}",
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
"grant_type": "refresh_token",
"refresh_token": REFRESH_TOKEN,
}
response = requests.post('https://api.dropboxapi.com/oauth2/token',
data=data,
auth=(APP_KEY, APP_SECRET))
access_token = response.json()["access_token"]
dbx = dropbox.Dropbox(access_token)
while True:
try:
with open('test.txt', 'rb') as f:
file_contents = f.read()
dbx.files_upload(file_contents, '/test.txt', mode=dropbox.files.WriteMode('overwrite'))
except dropbox.exceptions.AuthError as e:
headers = {
'Authorization': f"Basic {BASIC_AUTH}",
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
"grant_type": "refresh_token",
"refresh_token": REFRESH_TOKEN,
}
response = requests.post('https://api.dropboxapi.com/oauth2/token',
data=data,
auth=(APP_KEY, APP_SECRET))
access_token = response.json()["access_token"]
dbx = dropbox.Dropbox(access_token)
continue
except Exception as e:
print(f"An error occurred: {e}")
time.sleep(60)