modified: myjupyterlab.sh
[GalaxyCodeBases.git] / tools / wtb / obtain_token.py
blobec4f21f6c3b56c7dc7fa4443496f81f41e84bd94
1 #!/usr/bin/env python2
3 import argparse
4 import urllib
5 import urllib2
6 import urlparse
7 import base64
8 import json
9 import sys
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')
25 exit()
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:
35 f.write(access_token)
37 print 'Now you can use other twitter scripts.'