Here is the first method using urllib library
import urllib
import base64
import httplib</code></pre>
CONSUMER_KEY = 'my_key'
CONSUMER_SECRET = 'my_secret'
encoded_CONSUMER_KEY = urllib.quote(CONSUMER_KEY)
encoded_CONSUMER_SECRET = urllib.quote(CONSUMER_SECRET)
concat_consumer_url = encoded_CONSUMER_KEY + ":" + encoded_CONSUMER_SECRET
host = 'api.twitter.com'
url = '/oauth2/token/'
params = urllib.urlencode({'grant_type' : 'client_credentials'})
req = httplib.HTTPSConnection(host)
req.putrequest("POST", url)
req.putheader("Host", host)
req.putheader("User-Agent", "My Twitter 1.1")
req.putheader("Authorization", "Basic %s" % base64.b64encode(concat_consumer_url))
req.putheader("Content-Type" ,"application/x-www-form-urlencoded;charset=UTF-8")
req.putheader("Content-Length", "29")
req.putheader("Accept-Encoding", "utf-8")
req.endheaders()
req.send(params)
resp = req.getresponse()
print resp.status, resp.reason
print resp.read()
This is the Second Method using pycurl
import sys # used for the storage class
import pycurl # used for curling
import base64 # used for encoding string
import urllib # used for enconding
import cStringIO # used for curl buffer grabbing
import json # used for decoding json token
import time # used for stuff to do with the rate limiting
from time import sleep # used for rate limiting
from time import gmtime, strftime # used for gathering time
def get_bearer_token(consumer_key,consumer_secret):
# enconde consumer key
consumer_key = urllib.quote(consumer_key)
# encode consumer secret
consumer_secret = urllib.quote(consumer_secret)
# create bearer token
bearer_token = consumer_key+':'+consumer_secret
# base64 encode the token
base64_encoded_bearer_token = base64.b64encode(bearer_token)
# set headers
headers = [
"POST /oauth2/token HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: shobhit Twitter Application-only OAuth App Python v.1",
"Authorization: Basic "+base64_encoded_bearer_token+"",
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: 29"]
token_url = "https://api.twitter.com/oauth2/token"
buf = cStringIO.StringIO()
access_token = ''
pycurl_connect = pycurl.Curl()
pycurl_connect.setopt(pycurl_connect.URL, token_url) # used to tell which url to go to
pycurl_connect.setopt(pycurl_connect.WRITEFUNCTION, buf.write) # used for generating output
pycurl_connect.setopt(pycurl_connect.HTTPHEADER, headers) # sends the customer headers above
pycurl_connect.setopt(pycurl_connect.POSTFIELDS, 'grant_type=client_credentials')
pycurl_connect.perform() # perform the curl
access_token = buf.getvalue() # grab the data
pycurl_connect.close() # stop the curl
x = json.loads(access_token)
bearer = x['access_token']
return bearer
Tags: oauth2 twitter python, python oauth2 twitter, twitter application only oauth2 python, twitter oauth python application oly, twitter outh python, twitter python access token, twitter python bearer token, twitter python oauth access token