Skip to content

Commit 8711c8d

Browse files
add an option of generating short live dropbox token
1 parent adbff12 commit 8711c8d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

onlinejudge_verify/verify.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Python Version: 3.x
22
import hashlib
3+
import json
34
import math
45
import os
56
import pathlib
@@ -9,6 +10,8 @@
910
from logging import getLogger
1011
from typing import *
1112

13+
import requests
14+
1215
import onlinejudge
1316
import onlinejudge_verify.languages.list
1417
import onlinejudge_verify.marker
@@ -47,6 +50,19 @@ def exec_command(command: List[str]):
4750
os.chdir(str(cwd))
4851

4952

53+
def get_fresh_dropbox_token() -> str:
54+
data = {
55+
'grant_type': 'refresh_token',
56+
'refresh_token': os.environ['DROPBOX_REFRESH_TOKEN'],
57+
}
58+
response = requests.post('https://api.dropbox.com/oauth2/token', data=data, auth=(os.environ['DROPBOX_APP_KEY'], os.environ['DROPBOX_APP_SECRET']), timeout=30)
59+
try:
60+
data = response.json()
61+
return data.get('access_token', '')
62+
except json.JSONDecodeError:
63+
return ''
64+
65+
5066
def verify_file(path: pathlib.Path, *, compilers: List[str], tle: float, jobs: int) -> Optional[bool]:
5167
logger.info('verify: %s', path)
5268

@@ -81,6 +97,9 @@ def verify_file(path: pathlib.Path, *, compilers: List[str], tle: float, jobs: i
8197

8298
if os.environ.get('DROPBOX_TOKEN'):
8399
command += ['--dropbox-token', os.environ['DROPBOX_TOKEN']]
100+
# 短期間で失効するDROPBOX_TOKENを新規に取得するオプションを用意する
101+
elif os.environ.get('DROPBOX_REFRESH_TOKEN') and os.environ.get('DROPBOX_APP_KEY') and os.environ.get('DROPBOX_APP_SECRET'):
102+
command += ['--dropbox-token', get_fresh_dropbox_token()]
84103
if os.environ.get('YUKICODER_TOKEN'):
85104
command += ['--yukicoder-token', os.environ['YUKICODER_TOKEN']]
86105
try:

0 commit comments

Comments
 (0)