11 parser
= argparse
.ArgumentParser(description
='Obtain access token from Twitter')
13 parser
.add_argument('-c', '--consumer-key', action
='store', dest
='consumer_key', help='Consumer key')
14 parser
.add_argument('-s', '--consumer-secret', action
='store', dest
='consumer_secret', help='Consumer key')
15 parser
.add_argument('-u', '--api-url', action
='store', dest
='api_url', help='API URL',
16 default
='https://api.twitter.com')
18 if len(sys
.argv
) == 1:
19 sys
.argv
.append('--help')
21 args
= parser
.parse_args()
23 if not args
.consumer_key
or not args
.consumer_secret
:
24 parser
.error('Consumer key and consumer secret required')
27 auth_header
= 'Basic %s' % base64
.urlsafe_b64encode('%s:%s' % (args
.consumer_key
, args
.consumer_secret
))
29 url
= urlparse
.urljoin(args
.api_url
, 'oauth2/token')
30 req
= urllib2
.Request(url
, data
=urllib
.urlencode({'grant_type': 'client_credentials'}),
31 headers
={'Authorization': auth_header
})
32 access_token
= json
.load(urllib2
.urlopen(req
))['access_token']
34 with
open('.twitter_token', 'w') as f
:
37 print 'Now you can use other twitter scripts.'