Piyush
Piyush

Reputation: 521

How to create twitter oauth signature while calling request_token url?

I want to generate oauth signature for twitter https://api.twitter.com/oauth/request_token url. I have developed following code but it will give me message like "Could not authenticate you". Please help me.

from hashlib import sha1
import hmac
import urllib
import time
import requests

oauth_nonce = "40575512181348616041501137897"
oauth_timestamp = str(int(time.time()))
oauth_consumer_key = ""
consumer_secret = ""
oauth_callback = ""

url = 'https://api.twitter.com/oauth/request_token'

test_str = "oauth_nonce="+oauth_nonce+"&oauth_callback="+oauth_callback+"&oauth_signature_method=HMAC-SHA1&oauth_timestamp="+oauth_timestamp+"&oauth_consumer_key="+oauth_consumer_key+"&oauth_version=1.0"

test_str = urllib.quote(test_str, safe='')
base_string = "POST&"+test_str

key = consumer_secret + "&"
hashed = hmac.new(key, base_string, sha1)

demo_str = hashed.digest().encode("base64").rstrip('\n')

oauth_signture = urllib.quote(demo_str, safe='')

print oauth_signture

client = requests.Session()
client.headers.update({'Authorization' : 'OAuth oauth_nonce="40575512181348616041501137897", oauth_timestamp="'+oauth_timestamp+'", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="'+oauth_consumer_key+'", oauth_signature="'+oauth_signture+'",oauth_callback="http%3A%2F%2Flocalhost%3A8069%2Fcallback'})

resp = client.post(url)    
print resp.content

Upvotes: 0

Views: 411

Answers (0)

Related Questions