From 7a8f4be10202372af33d0b160b16110d93752b83 Mon Sep 17 00:00:00 2001 From: jonathan vanasco Date: Tue, 22 Jan 2013 19:55:04 -0500 Subject: [PATCH 1/4] added ability to turn off ssl_verification in the 'requests' library ( on twython current, not twython3k ). ssl verification can often fail in development environments. ( there's a ton of Stack Overflow questions on this ) --- twython/twython.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/twython/twython.py b/twython/twython.py index 49c2f65..ae86fc6 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -82,7 +82,7 @@ class TwythonRateLimitError(TwythonError): class Twython(object): def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, \ - headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1'): + headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1' , ssl_verify=True ): """Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below). :param app_key: (optional) Your applications key @@ -92,6 +92,7 @@ class Twython(object): :param headers: (optional) Custom headers to send along with the request :param callback_url: (optional) If set, will overwrite the callback url set in your application :param proxies: (optional) A dictionary of proxies, for example {"http":"proxy.example.org:8080", "https":"proxy.example.org:8081"}. + :param ssl_verify: (optional, default True) Turns off ssl verifiction, for when you have development server issues. """ # Needed for hitting that there API. @@ -101,6 +102,7 @@ class Twython(object): self.access_token_url = self.api_url % 'oauth/access_token' self.authorize_url = self.api_url % 'oauth/authorize' self.authenticate_url = self.api_url % 'oauth/authenticate' + self.ssl_verify = ssl_verify # Enforce unicode on keys and secrets self.app_key = app_key and unicode(app_key) or twitter_token and unicode(twitter_token) @@ -268,7 +270,7 @@ class Twython(object): if self.callback_url: request_args['oauth_callback'] = self.callback_url - response = self.client.get(self.request_token_url, params=request_args) + response = self.client.get(self.request_token_url, params=request_args, verify=self.ssl_verify) if response.status_code != 200: raise TwythonAuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (response.status_code, response.content)) @@ -294,7 +296,7 @@ class Twython(object): def get_authorized_tokens(self): """Returns authorized tokens after they go through the auth_url phase. """ - response = self.client.get(self.access_token_url) + response = self.client.get(self.access_token_url, verify=self.ssl_verify) authorized_tokens = dict(parse_qsl(response.content)) if not authorized_tokens: raise TwythonError('Unable to decode authorized tokens.') @@ -496,7 +498,7 @@ class Twython(object): endpoint = 'users/profile_image/%s' % username url = self.api_url % version + '/' + endpoint - response = self.client.get(url, params={'size': size}, allow_redirects=False) + response = self.client.get(url, params={'size': size}, allow_redirects=False, verify=self.ssl_verify) image_url = response.headers.get('location') if response.status_code in (301, 302, 303, 307) and image_url is not None: From 2b2bb40913a6b2e4b068fe474a6cc49bceffcc89 Mon Sep 17 00:00:00 2001 From: jonathan vanasco Date: Tue, 23 Apr 2013 12:01:49 -0400 Subject: [PATCH 2/4] - fixed merge issue ( how did i miss that?!?) - added name to contributors --- README.md | 45 +++++++++++++++++++++++---------------------- twython/twython.py | 6 +----- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c3fcca5..9d99e78 100644 --- a/README.md +++ b/README.md @@ -173,25 +173,26 @@ This is a list of all those who have contributed code to Twython in some way, sh exhaustive, but I could be wrong - if you think your name should be here and it's not, please contact me and let me know (or just issue a pull request on GitHub, and leave a note about it so I can just accept it ;)). -- **[Mike Helmick (michaelhelmick)](https://github.com/michaelhelmick)**, multiple fixes and proper `requests` integration. -- **[kracekumar](https://github.com/kracekumar)**, early `requests` work and various fixes. -- **[Erik Scheffers (eriks5)](https://github.com/eriks5)**, various fixes regarding OAuth callback URLs. -- **[Jordan Bouvier (jbouvier)](https://github.com/jbouvier)**, various fixes regarding OAuth callback URLs. -- **[Dick Brouwer (dikbrouwer)](https://github.com/dikbrouwer)**, fixes for OAuth Verifier in `get_authorized_tokens`. -- **[hades](https://github.com/hades)**, Fixes to various initial OAuth issues and updates to `Twython3k` to stay current. -- **[Alex Sutton (alexdsutton)](https://github.com/alexsdutton/twython/)**, fix for parameter substitution regular expression (catch underscores!). -- **[Levgen Pyvovarov (bsn)](https://github.com/bsn)**, Various argument fixes, cyrillic text support. -- **[Mark Liu (mliu7)](https://github.com/mliu7)**, Missing parameter fix for `addListMember`. -- **[Randall Degges (rdegges)](https://github.com/rdegges)**, PEP-8 fixes, MANIFEST.in, installer fixes. -- **[Idris Mokhtarzada (idris)](https://github.com/idris)**, Fixes for various example code pieces. -- **[Jonathan Elsas (jelsas)](https://github.com/jelsas)**, Fix for original Streaming API stub causing import errors. -- **[LuqueDaniel](https://github.com/LuqueDaniel)**, Extended example code where necessary. -- **[Mesar Hameed (mhameed)](https://github.com/mhameed)**, Commit to swap `__getattr__` trick for a more debuggable solution. -- **[Remy DeCausemaker (decause)](https://github.com/decause)**, PEP-8 contributions. -- **[mckellister](https://github.com/mckellister)**, Fixes to `Exception`s raised by Twython (Rate Limits, etc). -- **[tatz_tsuchiya](http://d.hatena.ne.jp/tatz_tsuchiya/20120115/1326623451)**, Fix for `lambda` scoping in key injection phase. -- **[Voulnet (Mohammed ALDOUB)](https://github.com/Voulnet)**, Fixes for `http`/`https` access endpoints -- **[fumieval](https://github.com/fumieval)**, Re-added Proxy support for 2.3.0. -- **[terrycojones](https://github.com/terrycojones)**, Error cleanup and Exception processing in 2.3.0. -- **[Leandro Ferreira](https://github.com/leandroferreira)**, Fix for double-encoding of search queries in 2.3.0. -- **[Chris Brown](https://github.com/chbrown)**, Updated to use v1.1 endpoints over v1 +- **[Mike Helmick (michaelhelmick)](https://github.com/michaelhelmick)**, multiple fixes and proper `requests` integration. +- **[kracekumar](https://github.com/kracekumar)**, early `requests` work and various fixes. +- **[Erik Scheffers (eriks5)](https://github.com/eriks5)**, various fixes regarding OAuth callback URLs. +- **[Jordan Bouvier (jbouvier)](https://github.com/jbouvier)**, various fixes regarding OAuth callback URLs. +- **[Dick Brouwer (dikbrouwer)](https://github.com/dikbrouwer)**, fixes for OAuth Verifier in `get_authorized_tokens`. +- **[hades](https://github.com/hades)**, Fixes to various initial OAuth issues and updates to `Twython3k` to stay current. +- **[Alex Sutton (alexdsutton)](https://github.com/alexsdutton/twython/)**, fix for parameter substitution regular expression (catch underscores!). +- **[Levgen Pyvovarov (bsn)](https://github.com/bsn)**, Various argument fixes, cyrillic text support. +- **[Mark Liu (mliu7)](https://github.com/mliu7)**, Missing parameter fix for `addListMember`. +- **[Randall Degges (rdegges)](https://github.com/rdegges)**, PEP-8 fixes, MANIFEST.in, installer fixes. +- **[Idris Mokhtarzada (idris)](https://github.com/idris)**, Fixes for various example code pieces. +- **[Jonathan Elsas (jelsas)](https://github.com/jelsas)**, Fix for original Streaming API stub causing import errors. +- **[LuqueDaniel](https://github.com/LuqueDaniel)**, Extended example code where necessary. +- **[Mesar Hameed (mhameed)](https://github.com/mhameed)**, Commit to swap `__getattr__` trick for a more debuggable solution. +- **[Remy DeCausemaker (decause)](https://github.com/decause)**, PEP-8 contributions. +- **[mckellister](https://github.com/mckellister)**, Fixes to `Exception`s raised by Twython (Rate Limits, etc). +- **[tatz_tsuchiya](http://d.hatena.ne.jp/tatz_tsuchiya/20120115/1326623451)**, Fix for `lambda` scoping in key injection phase. +- **[Voulnet (Mohammed ALDOUB)](https://github.com/Voulnet)**, Fixes for `http`/`https` access endpoints +- **[fumieval](https://github.com/fumieval)**, Re-added Proxy support for 2.3.0. +- **[terrycojones](https://github.com/terrycojones)**, Error cleanup and Exception processing in 2.3.0. +- **[Leandro Ferreira](https://github.com/leandroferreira)**, Fix for double-encoding of search queries in 2.3.0. +- **[Chris Brown](https://github.com/chbrown)**, Updated to use v1.1 endpoints over v1 +- **[Jonathan Vanasco](https://github.com/jvanasco)**, Debugging support, error_code tracking, Twitter error API tracking, other fixes diff --git a/twython/twython.py b/twython/twython.py index 4a9b61c..5c51279 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -82,11 +82,7 @@ class TwythonRateLimitError(TwythonError): class Twython(object): def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, \ -<<<<<<< HEAD - headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1' , ssl_verify=True ): -======= - headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1.1'): ->>>>>>> d6f3d731a2a89a0add5b01b918cf61cf93f97d62 + headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1.1' , ssl_verify=True ): """Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below). :param app_key: (optional) Your applications key From bae92f235f655b8e0d7456b9941ee150d44f2175 Mon Sep 17 00:00:00 2001 From: jonathan vanasco Date: Tue, 23 Apr 2013 12:25:37 -0400 Subject: [PATCH 3/4] merged changes back in --- AUTHORS.rst | 3 ++- twython/twython.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index dff2a73..65ec451 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -35,6 +35,7 @@ Patches and Suggestions - `Terry Jones `_, Error cleanup and Exception processing in 2.3.0. - `Leandro Ferreira `_, Fix for double-encoding of search queries in 2.3.0. - `Chris Brown `_, Updated to use v1.1 endpoints over v1 -- `Virendra Rajput `_, Fixed unicode (json) encoding in twython.py 2.7.2. +- `Virendra Rajput `_, Fixed unicode (json) encoding in twython.py 2.7.2. - `Paul Solbach `_, fixed requirement for oauth_verifier - `Greg Nofi `_, fixed using built-in Exception attributes for storing & retrieving error message +- `Jonathan Vanasco `_, Debugging support, error_code tracking, Twitter error API tracking, other fixes diff --git a/twython/twython.py b/twython/twython.py index f6d0fc8..4a7cb03 100644 --- a/twython/twython.py +++ b/twython/twython.py @@ -34,7 +34,7 @@ except ImportError: class Twython(object): def __init__(self, app_key=None, app_secret=None, oauth_token=None, oauth_token_secret=None, - headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1.1'): + headers=None, callback_url=None, twitter_token=None, twitter_secret=None, proxies=None, version='1.1', ssl_verify=True ): """Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below). :param app_key: (optional) Your applications key @@ -44,6 +44,7 @@ class Twython(object): :param headers: (optional) Custom headers to send along with the request :param callback_url: (optional) If set, will overwrite the callback url set in your application :param proxies: (optional) A dictionary of proxies, for example {"http":"proxy.example.org:8080", "https":"proxy.example.org:8081"}. + :param ssl_verify: (optional, default True) Turns off ssl verifiction, for when you have development server issues. """ # Needed for hitting that there API. @@ -52,6 +53,7 @@ class Twython(object): self.request_token_url = self.api_url % 'oauth/request_token' self.access_token_url = self.api_url % 'oauth/access_token' self.authenticate_url = self.api_url % 'oauth/authenticate' + self.ssl_verify = ssl_verify # Enforce unicode on keys and secrets self.app_key = app_key and unicode(app_key) or twitter_token and unicode(twitter_token) @@ -238,7 +240,7 @@ class Twython(object): if self.callback_url: request_args['oauth_callback'] = self.callback_url - response = self.client.get(self.request_token_url, params=request_args) + response = self.client.get(self.request_token_url, params=request_args, verify=self.ssl_verify) if response.status_code == 401: raise TwythonAuthError(response.content, error_code=response.status_code) elif response.status_code != 200: @@ -271,7 +273,7 @@ class Twython(object): def get_authorized_tokens(self, oauth_verifier): """Returns authorized tokens after they go through the auth_url phase. """ - response = self.client.get(self.access_token_url, params={'oauth_verifier': oauth_verifier}) + response = self.client.get(self.access_token_url, params={'oauth_verifier': oauth_verifier}, verify=self.ssl_verify ) authorized_tokens = dict(parse_qsl(response.content)) if not authorized_tokens: raise TwythonError('Unable to decode authorized tokens.') From 62b984eb61e052ca06796c365e5f87fc10c63fac Mon Sep 17 00:00:00 2001 From: jonathan vanasco Date: Tue, 23 Apr 2013 12:29:01 -0400 Subject: [PATCH 4/4] added CHANGES.txt --- CHANGES.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 CHANGES.txt diff --git a/CHANGES.txt b/CHANGES.txt new file mode 100644 index 0000000..6947a32 --- /dev/null +++ b/CHANGES.txt @@ -0,0 +1,4 @@ +2013-04-23 (jvanasco) + - added ssl_verify param to Twython config. + option defaults to `True` and is proxied to the `requests` library client + can be set to `False`, which better supports local testing