简体   繁体   中英

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"}

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/ )

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM