I’m using python3 to send a request to the Twitter Search API. I’m testing these requests in Postman, and then using the code snippit generator to copy and paste into my editor to run like so:
url = "https://api.twitter.com/1.1/search/tweets.json?q=twitter"
payload={}
headers = {
'Authorization': 'OAuth oauth_consumer_key="",oauth_token="",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1613517432",oauth_nonce="x",oauth_version="1.0",oauth_body_hash="",oauth_signature=""',
'Cookie': 'personalization_id=""; lang=en; guest_id='
}
twitter_response = requests.request("GET", url, headers=headers, data=payload)
The requests work just fine in Postman. But in my script, the request only works when the query parameter for the URL is "nasa". If I change it to anything else, I get error 32 Could not authenticate.
Is there some quirk about Python I don’t know about when it comes to the Twitter Search API? Any guidance would be helpful here.
2
Answers
I was able to find a way around this by using the Tweepy API wrapper, which solved my OAuth issues. The headers weren't getting formatted properly for the request.
Sounds like you’re copying and pasting the OAuth headers from the Twitter Dev docs instead of calculating them with every new query. I think the docs use NASA as an example search query.