3

I am trying to access an API with Python but the best I have reached so far is a 401 response (that I am not authenticated).

this is the API:
https://opendata-api.stib-mivb.be/Files/1.0/Gtfs

this is the code I used to get the 401 response:

import requests
response = requests.get("https://opendata-api.stib-mivb.be/Files/1.0/Gtfs")
print (response.status_code)

I tried to understand the code example they give but cant get it:

curl -k -X GET --header "Accept: application/zip" --header "Authorization: Bearer b2ba6c7a35d667564ffa2765aec6ea07" -o ./gtfs.zip "https://opendata-api.stib-mivb.be/Files/1.0/Gtfs"

how to identify with the Consumer key, Consumer Secret , Acess Token I receive (they dont give Access Token Secret) With Tweepy you are also supposed to use Access Token Secret... for example:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

So there I am stuck...

Any help?

Thanks

chris pash
  • 31
  • 2
  • http://docs.python-requests.org/en/master/user/quickstart/#custom-headers - these docs show how to add headers to your request with the `requests` module – Jonathon McMurray Jan 19 '18 at 18:13

1 Answers1

0

The access method here is bearer token. You are provided a token that you keep secret and give back when you make requests, that's all there is to it. Just make your request like so:

response = requests.get("https://opendata-api.stib-mivb.be/Files/1.0/Gtfs",
    headers = {'Authorization': 'Bearer {}'.format(access_token)})

The data they give back is a good-sized zip file; I'd suggest following the example in this answer to stream the response to a file. Something like this:

response = requests.get("https://opendata-api.stib-mivb.be/Files/1.0/Gtfs",
    headers = {'Authorization': 'Bearer {}'.format(access_token)},
    stream = True)
with open('gtfs.zip', 'w') as out:
    for chunk in response.iter_content(chunk_size=4096):
        out.write(chunk)
Community
  • 1
  • 1
Nathan Vērzemnieks
  • 5,495
  • 1
  • 11
  • 23
  • thanks for your suggestions. I will take the time to analyse it but that s indeed the right direction (bearer token). – chris pash Jan 24 '18 at 18:21