Search code examples
oauth-2.0oauthpython-requestsxbrlsec

Having issue with XBRL API OAuth2


Has anyone worked with the XBRL API for financial data (https://xbrlus.github.io/xbrl-api/)? I am able to use the interactive demo but not able to recreate it on my own and successfully access the API using the python requests library when accessing the oauth2.

This is what I am trying now, which returns an "invalid request" error. Appreciate any support.

import requests

body_auth = {'username' : 'email@domain', 
            'client_id': 'generated from the XBRL API demo', 
            'client_secret' : 'generated from the XBRL API demo', 
            'password' : 'password', 
            'grant_type' : 'password'}

payload = urlencode(body_auth)
url = 'https://api.xbrl.us/oauth2/token'
headers = {"Content-Type": "application/x-www-form-urlencoded"}

res = requests.request("POST", url, data=payload, headers=headers)
auth_json = res.json()
auth_json

Solution

  • If I modify your code to add:

    from urllib.parse import urlencode
    

    and

    res.raise_for_status()
    

    I get:

    requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.xbrl.us/oauth2/token
    

    I assume that this is what you are seeing when you say "invalid request" ?

    If I then update username, client_id, client_secret and password with my own credentials, your code works without further modification.

    It's odd that this is resulting in a 400 error rather than a 401, but I would infer that there is an issue with the credentials that you are using rather than the code.