Beginner Help [Python]

Hey guys,

I’m new to playing with APIs, I don’t have any computer science background but it’s something I’m very interested in. I’ve gone through the Codecademy Python lessons and API tutorials, so I have some know-how. I’m running into an issue where I’m getting an error saying I’m missing my subscription key. Would anyone be able to let me know if the following code would throw an error like that? I know the subscription key I’m using is correct, but obviously I haven’t pasted it here.

> import requests
> import pprint
> query_params = {‘Ocp-Apim-Subscription-Key’: ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxx’}
> endpoint = ‘https://www.haloapi.com/stats/h5/players/xxxxxxx/matches
> response = requests.get(endpoint, params= query_params)
> data = response.json()
> print response.status_code
> print data

You need to send the key in the headers instead of as a query parameter. Make a dictionary for the headers (like your query params) and modify your request call to this:
response = requests.get(endpoint, params=query_params, headers=headers)

Like anonymouse91 said; you need to send your API Key as a header, not as a query parameter.

The following shows how to use the ‘headers’ and ‘params’ correctly.

> import requests
> import pprint
>
> pp = pprint.PrettyPrinter(indent=4)
>
> # Get Matches for Furiousn00b
> uri = ‘https://www.haloapi.com/stats/h5/players/Furiousn00b/matches
> headers = {‘Ocp-Apim-Subscription-Key’: ‘00000000000000000000000000000000’}
>
> matches_response = requests.get(uri, headers=headers)
>
> print matches_response.url
> pp.pprint(matches_response.json())
>
> # Get Arena Service Record for Furiousn00b
> uri = ‘https://www.haloapi.com/stats/h5/servicerecords/arena
> headers = {‘Ocp-Apim-Subscription-Key’: ‘00000000000000000000000000000000’}
> params = {‘players’: ‘Furiousn00b’}
>
> service_record_response = requests.get(uri, params=params, headers=headers)
>
> print service_record_response.url
> pp.pprint(service_record_response.json())

Thanks a lot guys, really appreciate the help. I’m moving along slowly but surely haha

> 2533274793210642;4:
> Thanks a lot guys, really appreciate the help. I’m moving along slowly but surely haha

If you need help with anything add it in the thread here, I just finished my Python wrapper so I’d be happy to provide insight on any issues you have.