Python + bearer + search


#1

I got a bearer_token properly, and now trying to test search through GET request.

It fails with error: urllib2.HTTPError: HTTP Error 400: Bad Request

Should something like this work:

<python>
    req = urllib2.Request('http://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi&')
    req.add_header('User-Agent', 'test')
    req.add_header('Authorization: Bearer ', bearer_token)
    req.add_header("Accept-Encoding", "gzip")
    response1 = urllib2.urlopen(req)
</python>

Premium API endpoint
#2

Something like that generally should work but I think you might be accidentally setting an HTTP header with the “key” "Authorization: Bearer " rather than the key being “Authorization” and the value being "Bearer " + bearer_token


#3

Thanks Taylor!! :slight_smile:

Wrongly left http instead of https, and as you pointed Authorization key was wrong.

Corrected (in case someone needs):

query = urllib.urlencode([(‘q’,‘test query’)])

url = 'https://api.twitter.com/1.1/search/tweets.json?%s' % query
req = urllib2.Request(url)
req.add_header('Authorization', 'Bearer '+ bearer_token + '')
req.add_header('User-Agent', 'Test Twitter Client')