Created
March 8, 2018 09:24
-
-
Save nicklegr/6bd6db630ddaf5af00f87fb2eb2503a8 to your computer and use it in GitHub Desktop.
Twitterのbearer_tokenの取得 (Application-only Authentication)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| # 参考: http://ktkrhr.hatenablog.com/entry/2013/03/27/002447 | |
| import urllib | |
| import urllib2 | |
| import base64 | |
| import json | |
| oauth_consumer_key = "" | |
| oauth_consumer_secret = "" | |
| token_credential = urllib.quote(oauth_consumer_key) + ':' + urllib.quote(oauth_consumer_secret) | |
| credential = base64.b64encode(token_credential) | |
| url = 'https://api.twitter.com/oauth2/token' | |
| value = {'grant_type': 'client_credentials'} | |
| data = urllib.urlencode(value) | |
| req = urllib2.Request(url) | |
| req.add_header('Authorization', 'Basic ' + credential) | |
| req.add_header('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8') | |
| response = urllib2.urlopen(req, data) | |
| json_response = json.loads(response.read()) | |
| access_token = json_response['access_token'] | |
| print access_token |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment