-
Notifications
You must be signed in to change notification settings - Fork 1
/
getORCIDToken.py
74 lines (62 loc) · 2.18 KB
/
getORCIDToken.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
from decouple import config
import logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
logfile_format = logging.Formatter('%(asctime)s: %(name)s (%(funcName)s): %(message)s')
logfile = logging.FileHandler(filename='getORCIDtoken.log')
logfile.setFormatter(logfile_format)
logfile.setLevel(logging.DEBUG)
log.addHandler(stream_handler)
log.addHandler(logfile)
testing = False
if testing:
ORCID_API_ID = config('ORCID_SANDBOX_API_ID')
ORCID_API_SECRET = config('ORCID_SANDBOX_API_SECRET')
else:
ORCID_API_ID = config('ORCID_API_ID')
ORCID_API_SECRET = config('ORCID_API_SECRET')
def fetchToken(id, secret, sandbox):
auth = {
'client_id': id,
'client_secret': secret,
'grant_type': 'client_credentials',
'scope': '/read-public'
}
headers = {
'accept': 'application/json'
}
if sandbox == True:
service = 'sandbox.'
log.info('In testing mode (using sandbox)')
else:
service = ''
log.info('In production mode')
tokenURL = 'https://' + service + 'orcid.org/oauth/token'
try:
response = requests.post( url=tokenURL,
headers=headers,
data=auth )
response.raise_for_status()
if response.status_code == 200:
ORCID_ACCESS_TOKEN = response.json()['access_token']
log.info('Retrieving token from {0}...'.format(tokenURL))
log.debug('Response code {0}'.format(response.status_code))
return ORCID_ACCESS_TOKEN
except requests.RequestException as error:
log.exception(error)
raise
def writeToken(token, testing):
with open(".env", "a") as file:
if testing == True:
file.write("\n# Sandbox token\n")
token_env = ('\nPUBLIC_ACCESS_TOKEN = \"{0}\"'.format(token))
file.write(token_env)
log.info("Added token to .env file.")
file.close
if __name__ == '__main__':
token = fetchToken(ORCID_API_ID, ORCID_API_SECRET, testing)
log.info("Fetched token.")
writeToken(token, testing)