Skip to content

Instantly share code, notes, and snippets.

@yuitest
Created May 5, 2014 13:26
Show Gist options
  • Save yuitest/71a4d922433e7b3b42be to your computer and use it in GitHub Desktop.
Save yuitest/71a4d922433e7b3b42be to your computer and use it in GitHub Desktop.
Python で Twitter の XAuth するサンプルコード。
# 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