bear / python-twitter

A Python wrapper around the Twitter API.
Apache License 2.0
3.41k stars 957 forks source link

Documentation to help new users avoid "Only unicode objects are escapable" error #637

Closed chmreid closed 4 years ago

chmreid commented 4 years ago

The problem with the current documentation

On this page of the documentation (https://python-twitter.readthedocs.io/en/latest/getting_started.html#getting-your-application-tokens), the following bit of example code is given:

import twitter
api = twitter.Api(consumer_key=[consumer key],
                  consumer_secret=[consumer secret],
                  access_token_key=[access token],
                  access_token_secret=[access token secret])

The use of the square brackets is overloaded, as [consumer key] in Python indicates a list containing a variable with your consumer key, whereas the intention of the documentation is for the entire bit, square brackets too, to be replaced with a string.

The not-good outcome

The not-good outcome is that if a new user trying to use twitter-api for the first time accidentally puts ["AAAAAAAA"] instead of "AAAAAAAAAA" for their consumer/access token key/secret, they get a very cryptic error message that gives no indication of how to fix the problem:

<...very long traceback...>

~/.pyenv/versions/miniconda3-4.3.30/lib/python3.6/site-packages/oauthlib/oauth1/rfc5849/utils.py in escape(u)
     55     if not isinstance(u, unicode_type):
     56         raise ValueError('Only unicode objects are escapable. ' +
---> 57                          'Got %r of type %s.' % (u, type(u)))
     58     # Letters, digits, and the characters '_.-' are already treated as safe
     59     # by urllib.quote(). We need to add '~' to fully support rfc5849.

ValueError: Only unicode objects are escapable. Got <generator object to_unicode.<locals>.<genexpr> at 0x111411518> of type <class 'generator'>.

Proposed solution

I propose replacing this code example with:

import twitter
api = twitter.Api(consumer_key=<consumer key>,
                  consumer_secret=<consumer secret>,
                  access_token_key=<access token],
                  access_token_secret=<access token>)

or better yet include some code from one of the examples: https://github.com/bear/python-twitter/blob/master/examples/view_friends.py

import twitter

CONSUMER_KEY = 'WWWWWWWW'
CONSUMER_SECRET = 'XXXXXXXX'
ACCESS_TOKEN = 'YYYYYYYY'
ACCESS_TOKEN_SECRET = 'ZZZZZZZZ'

api = twitter.Api(consumer_key=CONSUMER_KEY,
                  consumer_secret=CONSUMER_SECRET,
                  access_token_key=ACCESS_TOKEN,
                  acces