Home > other >  Python - Request not authorized by using requests and json
Python - Request not authorized by using requests and json

Time:10-18

I've been using code python to access to this kind of url https://api.elsevier.com/analytics/scival/institution/metrics?metricTypes=ScholarlyOutput&institutionIds=508175,508076&yearRange=5yrs&includeSelfCitations=true&byYear=true&includedDocs=AllPublicationTypes&journalImpactType=CiteScore&showAsFieldWeighted=false&apiKey=7f59af901d2d86f78a1fd60c1bf9426a

This is a public json url given by Elsevier Developer Portal

The little code I've been using is this:

import requests
import json

url = "https://api.elsevier.com/analytics/scival/institution/metrics?metricTypes=ScholarlyOutput&institutionIds=508175,508076&yearRange=5yrs&includeSelfCitations=true&byYear=true&includedDocs=AllPublicationTypes&journalImpactType=CiteScore&showAsFieldWeighted=false&apiKey=7f59af901d2d86f78a1fd60c1bf9426a"

with requests.Session() as s:
    data = s.get(url).json()

Everything was ok until today that my project gives me error when executing this code, and if i only run s.get(url).text then I get the following message 'Request not authorized. Please specify a valid API key or HMAC signature.' I have to say that I even have a own api_key and access_token, but even using those I get the same message.

So, I want some help to manage to extract the data, due to the data still is in the link https://api.elsevier.com/analytics/scival/institution/metrics?metricTypes=ScholarlyOutput&institutionIds=508175,508076&yearRange=5yrs&includeSelfCitations=true&byYear=true&includedDocs=AllPublicationTypes&journalImpactType=CiteScore&showAsFieldWeighted=false&apiKey=7f59af901d2d86f78a1fd60c1bf9426a (just google and will see it). Hope some guide to do this, because until yesterday there were no errors.

CodePudding user response:

I have checked the docs and you are missing the header in the request.

The below code is working.

import requests

url = "https://api.elsevier.com/analytics/scival/institution/metrics?metricTypes=ScholarlyOutput&institutionIds=508175,508076&yearRange=5yrs&includeSelfCitations=true&byYear=true&includedDocs=AllPublicationTypes&journalImpactType=CiteScore&showAsFieldWeighted=false&apiKey=7f59af901d2d86f78a1fd60c1bf9426a"

payload={}
headers = {
  'Accept': 'application/json'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)
  • Related