Search code examples
pythonmacosevernote

Evernote Python API "Invalid header value" authentication error received when using Jupyter iPython on Mac


I'm having some difficulty using the Evernote Python API with Jupyter iPython from either a notebook or the command line. Strangely, I have been able to do this in the past, but now it seems to be broken. The weird part is this is new behavior on my MacBook only. This still works in Jupyter iPython on my PC. I suspect something has broken in my setup, but I'm not sure how to diagnose it further.

First, I was able to connect to the API on the Mac running the following .py file:

from evernote.api.client import EvernoteClient

dev_token = "my_dev_token_here"
client = EvernoteClient(token=dev_token)
noteStore = client.get_note_store()
nbs= noteStore.listNotebooks()
for nb in nbs: print nb.name

I get the expected output- a list of my Evernote notebooks.

Next, I tried issuing these commands in a ipython CLI instance. I get following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-3796a38811af> in <module>()
----> 1 ns = client.get_note_store()

/usr/local/lib/python2.7/site-packages/evernote-1.23.2-py2.7.egg/evernote/api/client.pyc in get_note_store(self)
     66     def get_note_store(self):
     67         user_store = self.get_user_store()
---> 68         note_store_uri = user_store.getNoteStoreUrl()
     69         store = Store(self.token, NoteStore.Client, note_store_uri)
     70         if not store:  # Trick for PyDev code completion

/usr/local/lib/python2.7/site-packages/evernote-1.23.2-py2.7.egg/evernote/api/client.pyc in delegate_method(*args, **kwargs)
    136                 return functools.partial(
    137                     targetMethod, authenticationToken=self.token
--> 138                 )(**dict(zip(arg_names, args)))
    139             else:
    140                 return targetMethod(*args, **kwargs)

/usr/local/lib/python2.7/site-packages/evernote-1.23.2-py2.7.egg/evernote/edam/userstore/UserStore.pyc in getNoteStoreUrl(self, authenticationToken)
    910      - authenticationToken
    911     """
--> 912     self.send_getNoteStoreUrl(authenticationToken)
    913     return self.recv_getNoteStoreUrl()
    914 

/usr/local/lib/python2.7/site-packages/evernote-1.23.2-py2.7.egg/evernote/edam/userstore/UserStore.pyc in send_getNoteStoreUrl(self, authenticationToken)
    919     args.write(self._oprot)
    920     self._oprot.writeMessageEnd()
--> 921     self._oprot.trans.flush()
    922 
    923   def recv_getNoteStoreUrl(self, ):

/usr/local/lib/python2.7/site-packages/evernote-1.23.2-py2.7.egg/thrift/transport/THttpClient.pyc in _f(*args, **kwargs)
    123             orig_timeout = socket.getdefaulttimeout()
    124             socket.setdefaulttimeout(args[0].__timeout)
--> 125             result = f(*args, **kwargs)
    126             socket.setdefaulttimeout(orig_timeout)
    127             return result

/usr/local/lib/python2.7/site-packages/evernote-1.23.2-py2.7.egg/thrift/transport/THttpClient.pyc in flush(self)
    148         self.__http.putheader('Content-Length', str(len(data)))
    149         for key, value in self.__headers.iteritems():
--> 150             self.__http.putheader(key, value)
    151         self.__http.endheaders()
    152 

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.pyc in putheader(self, header, *values)
   1033         for one_value in values:
   1034             if _is_illegal_header_value(one_value):
-> 1035                 raise ValueError('Invalid header value %r' % (one_value,))
   1036 
   1037         hdr = '%s: %s' % (header, '\r\n\t'.join(values))

ValueError: Invalid header value 'en-devtoken:V=2 / 1.23; Python / 2.7.11 (default, Jan 22 2016, 08:29:18) \n[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)];'

I get a similar result when running the code in a Jupyter iPython notebook. Thanks for any clues you can provide.


Solution

  • The Invalid header value error has to be fixed on the latest SDK, which is 1.25.2. Please upgrade SDK and try again.