From 28e975bd921f9645df29c1107d573918c97becec Mon Sep 17 00:00:00 2001 From: Ivo Wetzel Date: Wed, 10 Mar 2010 17:07:22 -0600 Subject: [PATCH] Implement xAuth support. xAuth allows you to exchange an username and password pair for an OAuth access token. See http://apiwiki.twitter.com/Twitter-REST-API-Method:-oauth-access_token-for-xAuth for more details. --- tweepy/auth.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tweepy/auth.py b/tweepy/auth.py index 66fcfad..3593bec 100644 --- a/tweepy/auth.py +++ b/tweepy/auth.py @@ -49,8 +49,8 @@ class OAuthHandler(AuthHandler): self.username = None self.secure = secure - def _get_oauth_url(self, endpoint): - if self.secure: + def _get_oauth_url(self, endpoint, secure=False): + if self.secure or secure: prefix = 'https://' else: prefix = 'http://' @@ -125,6 +125,32 @@ class OAuthHandler(AuthHandler): except Exception, e: raise TweepError(e) + def get_xauth_access_token(self, username, password): + """ + Get an access token from an username and password combination. + In order to get this working you need to create an app at + http://twitter.com/apps, after that send a mail to api@twitter.com + and request activation of xAuth for it. + """ + try: + url = self._get_oauth_url('access_token', secure=True) # must use HTTPS + request = oauth.OAuthRequest.from_consumer_and_token( + oauth_consumer=self._consumer, + http_method='POST', http_url=url, + parameters = { + 'x_auth_mode': 'client_auth', + 'x_auth_username': username, + 'x_auth_password': password + } + ) + request.sign_request(self._sigmethod, self._consumer, None) + + resp = urlopen(Request(url, data=request.to_postdata())) + self.access_token = oauth.OAuthToken.from_string(resp.read()) + return self.access_token + except Exception, e: + raise TweepError(e) + def get_username(self): if self.username is None: api = API(self) -- 2.25.1