ryanmcgrath / twython

Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs.
http://stackoverflow.com/questions/tagged/twython
MIT License
1.85k stars 397 forks source link

send_direct_message and ValueError: Only unicode objects are escapable. #480

Open patricktokeeffe opened 6 years ago

patricktokeeffe commented 6 years ago

I'm following the simplest examples but cannot get direct messages working.

With a conf.py containing (well, with real keys):

#visit http://dev.twitter.com to create an application and get your keys
consumer_key =          'xxxxxxxxxxxxxxxxxxxx',
consumer_secret =       'xxxxxxxxxxxxxxxxxxxx',
access_token =          'xxxxxxxxxxxxxxxxxxxx',
access_token_secret =   'xxxxxxxxxxxxxxxxxxxx',

This script fails:

from conf import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)
twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)
twitter.send_direct_message(screen_name="patricktokeeffe", text="first test!")

..with this traceback:

Traceback (most recent call last):

  File "<ipython-input-4-8c788ca363df>", line 1, in <module>
    twitter.send_direct_message(screen_name="patricktokeeffe", text="first test!")

  File "C:\WinPython\python-2.7.9\lib\site-packages\twython\endpoints.py", line 315, in send_direct_message
    return self.post('direct_messages/new', params=params)

  File "C:\WinPython\python-2.7.9\lib\site-packages\twython\api.py", line 268, in post
    return self.request(endpoint, 'POST', params=params, version=version)

  File "C:\WinPython\python-2.7.9\lib\site-packages\twython\api.py", line 258, in request
    api_call=url)

  File "C:\WinPython\python-2.7.9\lib\site-packages\twython\api.py", line 159, in _request
    response = func(url, **requests_args)

  File "C:\WinPython\python-2.7.9\lib\site-packages\requests\sessions.py", line 504, in post
    return self.request('POST', url, data=data, json=json, **kwargs)

  File "C:\WinPython\python-2.7.9\lib\site-packages\requests\sessions.py", line 447, in request
    prep = self.prepare_request(req)

  File "C:\WinPython\python-2.7.9\lib\site-packages\requests\sessions.py", line 378, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),

  File "C:\WinPython\python-2.7.9\lib\site-packages\requests\models.py", line 308, in prepare
    self.prepare_auth(auth, url)

  File "C:\WinPython\python-2.7.9\lib\site-packages\requests\models.py", line 492, in prepare_auth
    r = auth(self)

  File "C:\WinPython\python-2.7.9\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 80, in __call__
    unicode(r.url), unicode(r.method), r.body or '', r.headers)

  File "C:\WinPython\python-2.7.9\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 314, in sign
    ('oauth_signature', self.get_oauth_signature(request)))

  File "C:\WinPython\python-2.7.9\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 129, in get_oauth_signature
    uri, headers, body = self._render(request)

  File "C:\WinPython\python-2.7.9\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 211, in _render
    request.oauth_params, request.headers, realm=realm)

  File "C:\WinPython\python-2.7.9\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
    return target(params, *args, **kwargs)

  File "C:\WinPython\python-2.7.9\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
    escaped_value = utils.escape(value)

  File "C:\WinPython\python-2.7.9\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
    'Got %r of type %s.' % (u, type(u)))

ValueError: Only unicode objects are escapable. Got <generator object <genexpr> at 0x0DD30300> of type <type 'generator'>.

I think this parallels #121, but I can't tell what requests is choking on.

michaelhelmick commented 6 years ago

Can you pip freeze so I can see which versions of twython, requests and requests-oauthlib are being used?

patricktokeeffe commented 6 years ago

OK thanks for waiting. My requests is really old, turns out 👇. Unfortunately I won't have time to upgrade and retest until Friday.

C:\WinPython\python-2.7.9>pip freeze
Cython==0.21.1
ExifRead==2.0.0
Jinja2==2.7.3
MarkupSafe==0.23
Pillow==2.6.1
Polygon==2.0.5
PuLP==1.5.6
PyOpenGL==3.1.0
PyPDF2==1.24
PyQtdesignerplugins==1.1
PySide==1.2.2
PyYAML==3.11
Pygments==2.0.1
SQLAlchemy==0.9.8
Sphinx==1.2.3
TTFQuery==1.0.5
VPython==5.74
ViTables==2.1
XlsxWriter==0.6.4
astroid==1.3.2
backports-abc==0.4
backports.ssl-match-hostname==3.4.0.2
baresql==0.7.0
beautifulsoup4==4.3.2
bokeh==0.12.0
brewer2mpl==1.4.1
certifi==2016.2.28
colorama==0.3.2
configparser==3.5.0b2
coverage==3.7.1
cvxopt==1.1.7
cx-Freeze==4.3.3
db.py==0.3.5
decorator==3.4.0
docutils==0.12
enum34==1.0.4
fonttools==2.3
formlayout==1.0.15
futures==3.0.5
ggplot==0.6.5
guidata==1.6.2
guiqwt==2.3.2
h5py==2.3.1
husl==4.0.0
ipython==2.3.1
ipython-sql==0.3.4
joblib==0.8.4
julia==0.1.1.6
llvmpy==0.12.7
lmfit==0.8.0
logilab-common==0.63.2
logutils==0.3.3
lxml==3.4.1
mahotas==1.2.3
matplotlib==1.4.2
mpld3==0.2
mysql-connector-python==1.2.3
networkx==1.9.1
nltk==3.0.0
nose==1.3.4
numba==0.15.1
numexpr==2.4
numpy==1.8.2
oauthlib==2.0.6
oct2py==2.4.0
pandas==0.15.2
patsy==0.3.0
pdfmerge==0.0.7
pep8==1.5.7
pg8000==1.10.1
prettytable==0.7.2
psutil==2.1.3
pyflakes==0.8.1
pyhdf==0.8.3
pylint==1.4.0
pyodbc==3.0.7
pyparsing==2.0.3
pyreadline==2.0
pyserial==2.7
python-dateutil==2.3
pytz==2014.10
pywin32==219
pyzmq==14.4.1
reportlab==3.1.8
requests==2.5.0
requests-oauthlib==0.8.0
rope==0.10.2
rpy2==2.5.2
scikit-image==0.10.1
scikit-learn==0.15.2
scilab2py==0.5
scipy==0.14.0
seaborn==0.5.1
simplejson==3.6.5
singledispatch==3.4.0.3
six==1.8.0
spyder==2.3.2
sqlite-bro==0.8.7.4
sqlparse==0.1.14
statsmodels==0.6.1
sympy==0.7.6
tables==3.1.1
tornado==4.3
tqdm==1.0
twython==3.6.0
wheel==0.24.0
windrose==1.6
winpython==1.0
xlrd==0.9.3
olymk2 commented 6 years ago

Just hit this myself, with these versions on the most basic example.

requests==2.18.4
requests-oauthlib==0.8.0
python-twitter==3.4.1

using Python 3.6.4, doing the most basic postmessage example :/

Actually this seems to be in the twitter library itself