From: Ivo Wetzel Date: Wed, 10 Mar 2010 23:07:22 +0000 (-0600) Subject: Implement xAuth support. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=28e975bd921f9645df29c1107d573918c97becec;p=tweepy.git 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. --- 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)