HTTP 400 using Python Example

Hi

I'm trying the Python Example but getting a HTTP 400, what's missing?

https://developer.mypurecloud.com/api/tutorials/oauth-client-credentials/#python

Thanks in advance

A 400 response means "bad request". Can you provide some more info on what resource you're using, the response body, etc?

Hi

I'm using the example on the URL as is.

Can you explain why the example provided does not work?

Can you provide some more info on what resource you're using, the response body, etc? I don't have much to go on knowing that something somewhere is giving you a 400. There's multiple requests in the example and the response bodies typically contain information that's helpful for troubleshooting.

response = requests.post('https://login.mypurecloud.com/oauth/token', data=requestBody, headers=requestHeaders)

if response.status_code == 200:
	print 'Got token'
else:
	print 'Failure: ' + str(response.status_code) + ' - ' + response.reason
	sys.exit(response.status_code)
clientId = 'xxxx'
clientSecret = 'xxxxx'
authorization = base64.b64encode(clientId + ':' + clientSecret)

requestHeaders = {
	'Authorization': 'Basic ' + authorization,
	'Content-Type': 'application/x-www-form-urlencoded'
}
requestBody = {
	'grant_type': 'client_credentials'
}

response = requests.post('https://login.mypurecloud.com/oauth/token', data=requestBody, headers=requestHeaders)

if response.status_code == 200:
	print 'Got token'
else:
	print 'Failure: ' + str(response.status_code) + ' - ' + response.reason
	sys.exit(response.status_code)

are you trying to connect to the .com or .ie regions? If you aren't connecting to .com, you need to change https://login.mypurecloud.com to the correct region

After you switch to the correct region, I'd like to see the response you're getting if it's still not working. What does this code output?

if response.status_code == 200:
	print 'Got token'
else:
	print 'Failure: ' + str(response.status_code) + ' - ' + response.reason
	sys.exit(response.status_code)

Hi Yes that was the issue. Thanks