Home › Forums › The XBRL API › Getting error – 'Bad or expired token'
- This topic has 2 replies, 2 voices, and was last updated 6 years, 1 month ago by Sriram Srinivasan.
-
AuthorPosts
-
-
Tuesday, November 19, 2019 at 9:57 AM #157736Sriram SrinivasanParticipant
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.
-
Tuesday, November 19, 2019 at 4:57 PM #157795Marc WardKeymaster
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.
-
Wednesday, November 20, 2019 at 2:35 AM #157819Sriram SrinivasanParticipant
Thanks a lot Mark! This worked.
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.
Search Forums
Recent Topics
- Income Statement data for a company by David Tauriello
- Normalized data presentation by David Tauriello
- Extract Director RSUs/Options Form 14A by Priyanka Goonetilleke
- Using the offset parameter by David Tauriello
- Spreadsheet Commands: Query Worksheet and Query Workbook Query Workbook by Liam Noailles
Recent Replies
-
Income Statement data for a company 5 months, 3 weeks ago
-
Income Statement data for a company 5 months, 4 weeks ago
-
Using the offset parameter 10 months ago
-
Using the offset parameter 10 months ago
-
Using the offset parameter 10 months ago
Documentation & Discussion
- Get started with Google Sheets OR
- Get started with Microsoft Excel OR
- Get access to as-filed data from us for other tools or your own app
- XBRL API Interactive Documentation
- Ten Tips & Techniques
- The XBRL API
- XBRL Data Community
- 2024 US GAAP Taxonomy Viewer
- Live support - Monday, 3:30 - 4:30 PM ET
Who's using this free data?
API Use 2024 || API Use 2023 || API Use 2022 || API Use 2021 || API Use 2020 || API Use 2019 || API Use 2018
Join XBRL US
- Individual Options - Basic, Power User & Sole Practitioner
- For Your Team - Startup, Non-Profit, Academic & Corporate options
- Member Benefits Comparison Table
Using the XBRL API with the Public Filings Database
Unless otherwise agreed to in writing, any and all use of the XBRL API to authenticate and retrieve data from the XBRL US Database of Public Filings implies user consent and agreement with the XBRL US API Agreement. If you are unable to agree to these terms, do not use the XBRL API.
To use the XBRL API outside of Google Sheets, your account needs to be provisioned for OAuth2 access.





