Created
May 5, 2014 13:26
-
-
Save yuitest/71a4d922433e7b3b42be to your computer and use it in GitHub Desktop.
Python で Twitter の XAuth するサンプルコード。
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: utf8 | |
import oauth2 as oauth | |
import urllib | |
import urlparse | |
def xauth_to_oauth( | |
consumer_key, consumer_secret, screen_name, password, | |
API_URL='https://api.twitter.com/oauth/access_token', | |
): | |
consumer = oauth.Consumer(consumer_key, consumer_secret) | |
client = oauth.Client(consumer) | |
client.add_credentials(screen_name, password) | |
client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1() | |
resp, token = client.request( | |
API_URL, method="POST", body=urllib.urlencode({ | |
"x_auth_mode": 'client_auth', | |
"x_auth_username": screen_name, | |
"x_auth_password": password, | |
})) | |
oauth_tokens = dict(urlparse.parse_qsl(token)) | |
return oauth_tokens['oauth_token'], oauth_tokens['oauth_token_secret'] | |
if __name__ == '__main__': | |
CONSUMER_KEY = '<>' | |
CONSUMER_SECRET = '<>' | |
SCREEN_NAME = '<>' | |
PASSWORD = '<>' | |
print(xauth_to_oauth(CONSUMER_KEY, CONSUMER_SECRET, SCREEN_NAME, PASSWORD)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment