I am trying to follow the instructions for pulling data from market news api from USDA in python, https://mymarketnews.ams.usda.gov/mymarketnews-api/authentication, but I get a 401 error
import requests
url = 'https://marsapi.ams.usda.gov/services/v1.2/reports'
headers={'x-api-key':'mars_test_343343'}
resp = requests.get(url, headers= headers)
print(resp.status_code)
I can't get it to work, the documentation says " In your software, use the API key as the basic authentication username value. You do not need to provide a password. ", I am pretty new to API calls, how do I provide my username as the api-key so the server authenticates me?
Note: I am using my actual api-key in the code instead of 'mars_test_343343'
Basic Authentication works a little differently with the requests library. You can do something like this instead:
from requests.auth import HTTPBasicAuth
resp = requests.get(url, auth=HTTPBasicAuth('mars_test_343343', None)
Note that because of the vagueness of "not needing to provide a password", the None value may need to be an empty string '' instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With