Implement xAuth support.
authorIvo Wetzel <ivo.wetzel@googlemail.com>
Wed, 10 Mar 2010 23:07:22 +0000 (17:07 -0600)
committerJoshua Roesslein <jroesslein@gmail.com>
Wed, 10 Mar 2010 23:09:32 +0000 (17:09 -0600)
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

index 66fcfad0c2f4ae845fe483b1586cd8b490e314c1..3593bec9dc141cec017afdd5590ac5beb39e95bc 100644 (file)
@@ -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)