11import requests
2- import time
32from decouple import config
3+ import logging
44
5- ORCID_API_ID = config ('ORCID_API_ID' )
6- ORCID_API_SECRET = config ('ORCID_API_SECRET' )
5+ log = logging .getLogger (__name__ )
6+ log .setLevel (logging .DEBUG )
7+
8+ stream_handler = logging .StreamHandler ()
9+ stream_handler .setLevel (logging .INFO )
10+
11+ logfile_format = logging .Formatter ('%(asctime)s: %(name)s (%(funcName)s): %(message)s' )
12+ logfile = logging .FileHandler ( filename = 'getORCIDtoken.log' ,
13+ mode = 'a' ,
14+ encoding = None ,
15+ delay = False )
16+ logfile .setFormatter (logfile_format )
17+ logfile .setLevel (logging .DEBUG )
18+
19+ log .addHandler (stream_handler )
20+ log .addHandler (logfile )
721
822testing = False
923
24+ if testing :
25+ ORCID_API_ID = config ('ORCID_SANDBOX_API_ID' )
26+ ORCID_API_SECRET = config ('ORCID_SANDBOX_API_SECRET' )
27+ else :
28+ ORCID_API_ID = config ('ORCID_API_ID' )
29+ ORCID_API_SECRET = config ('ORCID_API_SECRET' )
30+
1031def fetchToken (id , secret , sandbox ):
1132 auth = {
1233 'client_id' : id ,
1334 'client_secret' : secret ,
1435 'grant_type' : 'client_credentials' ,
1536 'scope' : '/read-public'
16- }
37+ }
1738 headers = {
1839 'accept' : 'application/json'
1940 }
20-
41+
2142 if sandbox == True :
22- service = " sandbox."
23- print ( "NB: Running in testing mode (using sandbox)." )
43+ service = ' sandbox.'
44+ log . info ( 'In testing mode (using sandbox)' )
2445 else :
2546 service = ''
47+ log .info ('In production mode' )
2648
2749 tokenURL = 'https://' + service + 'orcid.org/oauth/token'
2850
29- response = requests . post (
30- url = tokenURL ,
31- headers = headers ,
32- data = auth
33- )
34-
35- print ( 'Retrieving token from {0}...' . format ( tokenURL ))
36-
37- if response . status_code == 200 :
38- ORCID_ACCESS_TOKEN = response . json ()[ 'access_token' ]
39- return ORCID_ACCESS_TOKEN
40- else :
41- raise Exception ( 'Error: {0} {1}' . format ( response . status_code , response . text ))
51+ try :
52+ response = requests . post ( url = tokenURL ,
53+ headers = headers ,
54+ data = auth )
55+ response . raise_for_status ( )
56+ if response . status_code == 200 :
57+ ORCID_ACCESS_TOKEN = response . json ()[ 'access_token' ]
58+ log . info ( 'Retrieving token from {0}...' . format ( tokenURL ))
59+ log . debug ( 'Response code {0}' . format ( response . status_code ))
60+ return ORCID_ACCESS_TOKEN
61+ except requests . RequestException as error :
62+ log . exception ( error )
63+ raise
4264
43- token = fetchToken (ORCID_API_ID , ORCID_API_SECRET , testing )
65+ def writeToken (token , testing ):
66+ with open (".env" , "a" ) as file :
67+ if testing == True :
68+ file .write ("\n # Sandbox token\n " )
69+ token_env = ('PUBLIC_ACCESS_TOKEN = \" {0}\" ' .format (token ))
70+ file .write (token_env )
71+ log .info ("Added token to .env file." )
72+ file .close
4473
45- with open (".env" , "a" ) as file :
46- newline = ("PUBLIC_ACCESS_TOKEN = \" {0}\" " .format (token ))
47- file .write (newline )
48- print ("Added token '{0}' to .env file." .format (token ))
49- file .close
74+ if __name__ == '__main__' :
75+ token = fetchToken (ORCID_API_ID , ORCID_API_SECRET , testing )
76+ log .info ("Fetched token." )
77+ writeToken (token , testing )
0 commit comments