def get_user_public_profile(self, user): session = requests.Session() key, secret = self.get_key_and_secret() api = orcid.PublicAPI(key, secret, sandbox=False) # Get token res = session.post(self.ACCESS_TOKEN_URL, data={ 'client_id': key, 'client_secret': secret, 'scope': '/read-public', 'grant_type': 'client_credentials' }, headers={'Accept': 'application/json'}) res.raise_for_status() access_token = res.json().get('access_token') uid = user.social_auth.get(provider=self.name).uid res = session.get( 'https://pub.orcid.org/v1.2/{0}/orcid-profile/'.format(uid), headers={ 'Content-Type': 'application/orcid+json', 'Authorization': 'Bearer ' + access_token }) res.raise_for_status() return res.json()
def get_works(user_token, orcid_id): api = orcid.PublicAPI(config.get("DEFAULT", "ORCID_CLIENT_ID"), config.get("DEFAULT", "ORCID_SECRET")) summary = api.read_record_public(orcid_id, "works", user_token) metadata = [] for work in summary["group"]: work_summary = work["work-summary"][0] work_metadata = dict() work_metadata["id"] = work_summary["put-code"] work_metadata["title"] = work_summary["title"]["title"]["value"] work_metadata["last-modified-date"] = work_summary[ "last-modified-date"]["value"] work_metadata["type"] = work_summary["type"] work_metadata["publication-year"] = work_summary["publication-date"][ "year"]["value"] work_metadata["owner"] = orcid_id for ext_id in work_summary["external-ids"]["external-id"]: if ext_id["external-id-type"] == "doi": work_metadata["doi"] = ext_id["external-id-value"] if ext_id["external-id-type"] == "pmid": work_metadata["pmid"] = ext_id["external-id-value"] if ext_id["external-id-type"] == "pmc": work_metadata["pmcid"] = ext_id["external-id-value"] metadata.append(work_metadata) return metadata
def get_orcid_record(orcid_id, token, request_type=''): """Get ORCID record details. It returns a dictionary with the following structure: .. code-block:: python { 'orcid-identifier': { 'path': '0000-0000-0000-0000', }, 'person': { 'name': { 'given-names': { 'value': 'John' }, 'family-name': { 'value': 'Doe' }, }, 'emails': { 'email': [{ 'email': '*****@*****.**', }], }, ... }, ... } :param orcid_id: (str) ORCID identifier. :param token: (str) Access token for querying API. :param request_type: (str) Request type for retreiving a specific section of the record. :returns: (dict|None) Dictionary of the Orcid record or none if something goes wrong """ credentials = current_app.config.get('ORCID_APP_CREDENTIALS') api = orcid.PublicAPI( credentials['consumer_key'], credentials['consumer_secret'], sandbox=(current_app.config.get('ORCID_DOMAIN').startswith('sandbox')) ) try: return api.read_record_public(orcid_id, request_type, token) except RequestException: return None
from requests import RequestException import orcid api = orcid.PublicAPI('APP-H43S7S70C6QGG5GY', '67c4ec09-c890-494a-b64b-7428e9edee90', sandbox=True) token = api.get_token_from_authorization_code(authorization_code, redirect_uri) #search_results = api.search_public('text:English') # Get the summary #token = api.get_token('APP-H43S7S70C6QGG5GY', '67c4ec09-c890-494a-b64b-7428e9edee90', redirect_uri) #summary = api.read_record_public('0000-0002-3117-5116', 'activities', token) #summary = api.read_record_public('0000-0002-3117-5116', 'record', token)
def get_dois_from_orcid_api_v2_1(orcid_id, get_titles=False, low_level=True): """new api v 2.1 parsing of works""" if not orcid_id: return [] works = None if low_level: token = CFG_OAUTH2_CONFIGURATIONS['orcid'].get('search_token', '') if not token: return [] headers = {'Content-type': 'application/vnd.orcid+json', 'Authorization type': 'Bearer', 'Access token': token} url = 'https://pub.orcid.org/v2.1/{0}/works'.format(orcid_id) @retry(wrap_exception=True, retry_on_exception=retry_if_timeout_error, wait_exponential_multiplier=250, stop_max_attempt_number=3) def _get_works_from_orcid(url, headers, timeout=16): res = requests.get(url, headers=headers, timeout=timeout) if not res.status_code == requests.codes.ok: res.raise_for_status() return res try: res = _get_works_from_orcid(url, headers) except RetryError: register_exception(alert_admin=True) return [] except Exception: register_exception(alert_admin=True) return [] try: works = res.json() except ValueError: register_exception(alert_admin=True) return [] else: # requires orcid.py version >= '1.0.0' import pkg_resources try: orcversion = pkg_resources.get_distribution("orcid").version except Exception: return [] if orcversion and orcversion.startswith(0): return [] try: sandbox = 'sandbox' in CFG_OAUTH2_CONFIGURATIONS['orcid']['member_url'] ap = orcid.PublicAPI( institution_key=CFG_OAUTH2_CONFIGURATIONS['orcid']['consumer_key'], institution_secret=CFG_OAUTH2_CONFIGURATIONS['orcid']['consumer_secret'], sandbox=sandbox, timeout=60) token = CFG_OAUTH2_CONFIGURATIONS['orcid'].get('search_token', '') if not token: token = ap.get_search_token_from_orcid() works = ap.read_record_public(orcid_id, 'works', token) except Exception: register_exception(alert_admin=True) if not works: return [] dois = [] for work in works.get('group', {}): if get_titles: wksummary = work.get('work-summary', []) for summary in wksummary: title = '' titles = summary.get('title', {}) if titles is not None: title = titles.get('title', {}).get('value', '') eids = summary.get('external-ids', {}) if eids is None: continue for eid in eids.get('external-id', []): if eid.get('external-id-type', '') == 'doi': doi = get_doi(eid.get('external-id-value', '')) if not doi: continue dois.append((doi, title)) else: for eid in work.get('external-ids', {}).get('external-id', []): if eid.get('external-id-type', '') == 'doi': doi = get_doi(eid.get('external-id-value', '')) if not doi: continue dois.append(doi) return dois
orcid_ids = [ "0000-0003-3008-253X", # Mark "0000-0003-1805-8617", # Alessandro "0000-0001-7078-2720", # Ben Boys "0000-0002-5248-1219", # Deniz "0000-0002-0225-5010", # Lawrence "0000-0002-5984-7295", # Toni "0000-0001-9690-0887", # Ieva "0000-0003-0982-6733", # Hussein "0000-0002-7927-8720", # Kangrui "0000-0003-1230-3028", # Connor "0000-0002-7503-2117", # Yannis "0000-0002-5112-7373", # Jan ] api = orcid.PublicAPI(os.environ['ORCID_CLIENT_ID'], os.environ['ORCID_CLIENT_SECRET']) search_token = api.get_search_token_from_orcid() pub_list = [] for orcid_id in orcid_ids: logging.info(f"Downloading publications for ORCID ID: {orcid_id}.") query_result = api.read_record_public(orcid_id, 'works', search_token) for orcid_entry in query_result['group']: put_code = orcid_entry['work-summary'][0]['put-code'] orcid_full_entry = api.read_record_public(orcid_id, 'work', search_token, put_code=put_code) logging.info(orcid_full_entry)
def __init__(self, client_id, client_secret): """ Set up the ORCID API and retrieve a search token. """ self.api = orcid.PublicAPI(client_id, client_secret, sandbox=False) self.search_token = self.api.get_search_token_from_orcid()
def connect_api(self): """Connects to the orcid api to provide handle for searching""" self.P_api = orcid.PublicAPI(KEY, SECRET)