pablinhoechel
pablinhoechel

Reputation: 103

How to connect with an API that requires username and password

I am trying to connect to the api as explained in http://api.instatfootball.com/ , It is supposed to be something like the following get /[lang]/data/[action].[format]?login=[login]&pass=[pass]. I know the [lang], [action] and [format] I need to use and I also have a login and password but don´t know how to access to the information inside the API.

If I write the following code:

import requests

r = requests.get('http://api.instatfootball.com/en/data/stat_params_players.json', auth=('login', 'pass'))
r.text

with the actual login and pass, I get the following output:

{"status":"error"}

Upvotes: 1

Views: 7951

Answers (2)

dh762
dh762

Reputation: 2429

This API requires authentication as parameters over an insecure connection, so be aware that this is highly lacking on the API part.

import requests

username = 'login'
password = 'password'
base_url = 'http://api.instatfootball.com/en/data/{endpoint}.json'


r = requests.get(base_url.format(endpoint='stat_params_players'), params={'login': username, 'pass': password})
data = r.json()
print(r.status_code)
print(r.text)

Upvotes: 1

forellenfuerst
forellenfuerst

Reputation: 143

You will need to make a http-request using the URL. This will return the requested data in the response body. Depending on the [format] parameter, you will need to decode the data from xml / json to a native Python object.

As rdas already commented, you can use the request library for python (https://requests.readthedocs.io/en/master/). You will also find some code samples there. It will also do proper decoding of JSON data.

If you want to play around with the API a bit, you can use a tool like Postman for testing and debugging your requests. (https://www.postman.com/)

Upvotes: 0

Related Questions