Home Forums The XBRL API Getting error – 'Bad or expired token'

Viewing 1 reply thread
  • Author
    Posts
    • #157736
      Sriram Srinivasan
      Participant

      Hello everyone,

      I’m trying to access XBRL data from Python using the API. I’m new to Python and working with APIs so I’m going through some teething pains and would be glad to have some assistance.

      As a first step, I had to get the access token, which I successfully managed to do in Python using the below code (all sensitive data is masked).


      import requests
      API_ENDPOINT = “https://api.xbrl.us/oauth2/token”

      data = {‘grant_type’:’password’,
      ‘client_id’:’xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx’,
      ‘client_secret’:’xxxx-xxxx-xxxx-xxxx-xxxxx’,
      ‘username’:’xxxxxxxxxxxxxxx@xxx.com’,
      ‘password’: ‘xxxxxxxxxx’}

      # sending post request and saving response as response object
      r = requests.post(url = API_ENDPOINT, data = data)

      # extracting response text
      pastebin_url = r.text
      print(pastebin_url)

      This gave me output including an access token and a refresh token. I’m now trying to use the access token to pull data.

      This is the code I have been working with:

      “import requests

      # api-endpoint
      URL = “https://api.xbrl.us/api/v1/fact/search?concept.local-name=Assets&period.fiscalyear=2017&period.fiscal.period=1Q&Entity.cik=0000001&fields=fact.value”

      PARAMS = {‘Host’:’api.xbrl.us’,
      ‘Authorization’: ‘Bearer<xxxxxxx-xxxxx-xxx-xxx-xxxxx>’}

      # sending get request and saving the response as response object
      r = requests.get(url = URL, params = PARAMS)

      # extracting data in json format
      data = r.json()
      print(data)

      This returns the error: ‘Bad or expired token’. I have tried getting a fresh access token and running the code again but I’m still getting the same error.

      Is there anything wrong with my code? Will be grateful for any guidance. Thanks in advance for your help.

    • #157795
      Marc Ward
      Keymaster

      Hi Sriram,

      You’re almost there! The token needs to submitted in the header of the request. You don’t need to submit any parameters since they’re in the url.

      Add this to your code:

      headerarray = {'Authorization' : 'Bearer xxxxxxx-xxxxx-xxx-xxx-xxxxx'}

      Then change your request line to:

      r = requests.get(url, headers=headerarray)

      You should receive the results of the query now.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.

Upcoming XBRL US Events

Webinar: SEC Rule – Tailored Shareholder Reports for Mutual Funds and ETFs
Wednesday, May 15, 2024

Domain Steering Committee Meeting
Tuesday, May 21, 2024

Communications & Services Steering Committee Meeting
Tuesday, May 21, 2024

Center for Data Quality Committee Meeting
Thursday, June 27, 2024

GovFin 2024: Municipal Reporting Workshop
Tuesday, July 30, 2024