# HG changeset patch # User dclinton@b35c0ba3-1128-0410-9219-0b39014e361d # Date 1248965853 0 # Branch dclinton # Node ID c05534ecbc504595d1beab06ced7c6ebad9bae70 # Parent fa53aa14dce846d4d88ed2448f9f13bc62ce17ba Implemented the VerifyCredentials api method diff -r fa53aa14dce846d4d88ed2448f9f13bc62ce17ba -r c05534ecbc504595d1beab06ced7c6ebad9bae70 twitter.py --- a/twitter.py Thu Jul 30 14:56:55 2009 +0000 +++ b/twitter.py Thu Jul 30 14:57:33 2009 +0000 @@ -29,6 +29,7 @@ import tempfile import textwrap import time +import httplib import urllib import urllib2 import urlparse @@ -936,6 +937,27 @@ self._CheckForTwitterError(data) return NewResultsFromJsonDict(data) + def VerifyCredentials(self): + '''Returns a twitter.User instance if the authenticating user is valid. + + Returns: + A twitter.User instance representing that user if the + credentials are valid, None otherwise. + ''' + if not self._username: + raise TwitterError("Api instance must first be given user credentials.") + url = 'http://twitter.com/account/verify_credentials.json' + try: + json = self._FetchUrl(url, no_cache=True) + except urllib2.HTTPError, http_error: + if http_error.code == httplib.UNAUTHORIZED: + return None + else: + raise http_error + data = simplejson.loads(json) + self._CheckForTwitterError(data) + return NewUserFromJsonDict(data) + def SetCredentials(self, username, password): '''Set the username and password for this instance