Python search 400 error with bearer token authentication


#1

I’ve got a very short python script that ends up with a 400 response, and I can’t for the life of me figure out why. Any help would be much appreciated:

import json
import requests
import urllib

twitter_app_credentials = {
	"api_key": (api key here),
	"api_secret_key": (secret key here)
}

response = requests.post("https://api.twitter.com/oauth2/token", headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}, data={"grant_type": "client_credentials"}, auth=(twitter_app_credentials["api_key"], twitter_app_credentials["api_secret_key"]))

if response.status_code != 200:
	print "Failed to obtain bearer token:"
	print response.status_code
	print response.content
	exit()

deserialized_response = response.json()

if deserialized_response["token_type"] != "bearer":
	print "POST: https://api.twitter.com/oauth2/token failed to return a bearer token."
	print "a \"" + deserialized_response["token_type"] + "\" token was returned instead."
	exit()

bearer_token = deserialized_response["access_token"]
print bearer_token

response = requests.get('https://api.twitter.com/1.1/search/tweets.json', data={"q": "test"}, headers={'Authentication': 'Bearer %s' % bearer_token})

Could someone tell me what I’m missing here?