2
votes

I'm trying to download one message using the GMail API. Below is my traceback: pdiracdelta@pdiracdelta-Laptop:~/GMail Metadata$ ./main.py

<oauth2client.client.OAuth2Credentials object at 0x7fd6306c4d30>
False
Traceback (most recent call last):
  File "./main.py", line 105, in <module>
    main()
  File "./main.py", line 88, in main
    service = discovery.build('gmail', 'v1', http=http)
  File "/usr/lib/python3/dist-packages/oauth2client/util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/googleapiclient/discovery.py", line 197, in build
    resp, content = http.request(requested_url)
  File "/usr/lib/python3/dist-packages/oauth2client/client.py", line 562, in new_request
    redirections, connection_type)
  File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 1138, in request
    headers = self._normalize_headers(headers)
  File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 1106, in _normalize_headers
    return _normalize_headers(headers)
  File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 194, in _normalize_headers
    return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.items()])
  File "/usr/lib/python3/dist-packages/httplib2/__init__.py", line 194, in <listcomp>
    return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.items()])
TypeError: sequence item 0: expected str instance, bytes found

And below is a snippet of code which produces the credential object and boolean print just before the Traceback. It confirms that the credentials object is valid and is being used as suggested by Google:

credentials = get_credentials()
print(credentials)
print(str(credentials.invalid))
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)

What is going wrong here? It seems to me that I am not at fault, since the problem can be traced back to service = discovery.build('gmail', 'v1', http=http) which uses nothing but valid information (implying one of the packages used further in the stack cannot handle this valid information). Is this a bug, or am I doing something wrong?

1
Looks like you need an decode("utf-8") somewhere - Padraic Cunningham
But the code that is "available" to me (i.e. my own code) provides only valid objects to the methods of those packages. UTF-8 decoding might be the solution, but I'm trying to figure out whether this is a bug or not. - PDiracDelta
I think that link example is for python2. - Padraic Cunningham
If you look at the line except FlowExchangeError, error: you can see it it definitely for python2, there are many other python2 specific parts that I see in there. The support for python3 is recent enough so all the doc examples are really more geared toward python2, to make it work you would need to decode in the httplib2 code - Padraic Cunningham
OK Thanks Padraic I will try to figure that out :-) - PDiracDelta

1 Answers

0
votes

UPDATE: it seems that the _normalize_headers function has now been patched. Updating your python version should fix the problem (I'm using 3.6.7 now).

Solved with help from Padraic Cunningham, who identified the problem as an encoding issue. I solved this problem by applying .decode('utf-8') to the header keys and values (headers is a dict) if they are bytes-type objects (which are apparently UTF-8 encoded) and transforming them into python3 strings. This is probably due to some python2/3 mixing in the google API.

The fix also includes changing all code from google API examples to python3 code (e.g. exception handling) but most importantly my workaround involves editing /usr/lib/python3/dist-packages/httplib2/__init__.py at lines 193-194, redefining the _normalize_headers(headers) function as:

def _normalize_headers(headers):
     for key in headers:
         # if not encoded as a string, it is ASSUMED to be encoded as UTF-8, as it used to be in python2.
         if not isinstance(key, str):
             newkey = key.decode('utf-8')
             headers[newkey] = headers[key]
             del headers[key]
             key = newkey
         if not isinstance(headers[key], str): 
             headers[key] = headers[key].decode('utf-8')
     return dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.items()])

WARNING: this workaround is obviously quite dirty as it involves editing files from the httplib2 package. If someone finds a better fix, please post it here.