API status_code = 400, how to fix that?

907 views Asked by At
import requests

consumer_key     = 'IAqfvoioh1NRfvjAsCGqGP7W49w5yO5X'

Consumer_secret = 'bF6mdAS7Yj1nNsCi'

consumer_creds = f"{consumer_key}:{Consumer_secret}"

method = "GET"
version = {'version': 'v1'}
package = {'package':['discovery', 'accounts']}
resource = {'resource': "attractions"}
random = '/K8vZ9175BhV'
typee = 'json'

url = "https://app.ticketmaster.com/discovery/v2/attractions/K8vZ9175BhV.json?apikey=vPNhGwstALCFCwpCdzXjKGjGg0bQcEym4"

url_api = "https://app.ticketmaster.com/{package}/{version}/{resource}/{random}.{typee}?apikey=**{consumer_key}"

r = requests.get(url_api, data = version, params = package, headers = resource)
r.json()
valid_request = r.status_code in range(200, 299)



url_params = {'id': 'id'}
query_parama = {'locale':'en'}

I'm using this as a source: https://developer.ticketmaster.com/products-and-docs/apis/discovery-api/v2/

I'm unsure how to fix it

1

There are 1 answers

0
user128029 On

First, please take esqew's advice. This has been up long enough you should probably just change all of your API credentials.

On to the question though... but don't keep reading unless you have changed your creds :)

I'm guessing when you use url it works and when you use url_api it does not, correct? A 400 response means the it's a poorly formed request. The server receiving the request isn't able to figure out what it even means.

In this case, you are passing in the string saved in url_api without actually substituting the values you want. Unless I'm misreading something, I don't think the params and headers work the way you expect them to. At least for now, just create the url you want and make the request.

So something like this:

import requests

consumer_key     = 'super_secret_key'

method = "GET"
version = "v1"
package = "discovery"
resource = "attractions"
random = 'K8vZ9175BhV'
data_type = 'json'


url_api = f"https://app.ticketmaster.com/{package}/{version}/{resource}/{random}.{data_type}"

print(f"Making request with URL: {url_api}")

r = requests.get(url_api, params={'apikey': consumer_key})
...

I first made the string an f-string, so that those variables populate and then made the variables the string value you want to substitute in. Was your original goal to have requests fill in those variables? I haven't been using requests at all recently, so maybe there is a more elegant way to do this.

Note that the print statement does not include the key. By passing it to params it will not show up on the console. Which reminds me... you did change your API key, right?!?!

I would like to note again that I haven't used requests in a while. I hope I didn't confuse any of the syntax or misunderstand what you were going for in the original code.