cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: files_list_folder encoding issue

files_list_folder encoding issue

sparkie127
Explorer | Level 3
Go to solution

I've tried to use the sample code provided in other discussions to no avail. 

 

#!/usr/bin/env python
import dropbox

dbx = dropbox.Dropbox("***TOKEN*GOES*HERE***")

myDir = dbx.files_list_folder("")
for item in myDir.entries:
  if isinstance(item, dropbox.files.FileMetadata):
    name = item.name
    fileID = item.id
    fileHash = item.content_hash
    print(name, fileID, fileHash)

but I get:

Traceback (most recent call last):
  File "dropbox_test.py", line 51, in <module>
    myDir = dbx.files_list_folder("")
  File "/usr/local/lib/python3.8/dist-packages/dropbox/base.py", line 2118, in files_list_folder
    r = self.request(
  File "/usr/local/lib/python3.8/dist-packages/dropbox/dropbox.py", line 311, in request
    res = self.request_json_string_with_retry(host,
  File "/usr/local/lib/python3.8/dist-packages/dropbox/dropbox.py", line 461, in request_json_string_with_retry
    return self.request_json_string(host,
  File "/usr/local/lib/python3.8/dist-packages/dropbox/dropbox.py", line 557, in request_json_string
    r = self._session.post(url,
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
    httplib_response = self._make_request(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 387, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.8/http/client.py", line 1240, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1281, in _send_request
    self.putheader(hdr, value)
  File "/usr/lib/python3.8/http/client.py", line 1213, in putheader
    values[i] = one_value.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2014' in position 46: ordinal not in range(256)

I deleted everything in my dropbox account thinking maybe it was a bad folder name/encoding, but still getting this.

 

I saw another issue related to this, from a while back, but I don't think they are related, unless this is a new issue. (https://github.com/dropbox/dropbox-sdk-python/issues/71)

 

Using:

Python 3.8.2

dropbox 10.2.0

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

This code looks correct, and doesn't fail for me when I plug in my own access token.

 

Looking at the stack trace, it appears to be failing when making the request, specifically when writing a particular header. The only header here that would be varying though is the Authorization header with your access token. Also, the character it's failing on seems to be the unicode character for an em dash. So, it sounds like your access token string has unicode in it, when it should only contain ASCII. (Perhaps you opened or saved it in a text editor that automatically performed some conversion?)

 

To fix this, replace any non-ASCII characters in your access token with the correct ASCII character, such as the ASCII dash "-", or just get a new access token and use it as is.

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

This code looks correct, and doesn't fail for me when I plug in my own access token.

 

Looking at the stack trace, it appears to be failing when making the request, specifically when writing a particular header. The only header here that would be varying though is the Authorization header with your access token. Also, the character it's failing on seems to be the unicode character for an em dash. So, it sounds like your access token string has unicode in it, when it should only contain ASCII. (Perhaps you opened or saved it in a text editor that automatically performed some conversion?)

 

To fix this, replace any non-ASCII characters in your access token with the correct ASCII character, such as the ASCII dash "-", or just get a new access token and use it as is.

sparkie127
Explorer | Level 3
Go to solution

That was the problem... My token had a em dash in it... Thank you!!!

Need more support?