Search code examples
google-keep

Google Keep: gkeepapi -> can't add labels


Anybody else having trouble with adding labels to notes in gkeepapi?

import gkeepapi
keep = gkeepapi.Keep()

file = open("C:\\xxxxxxx", "r")
pwd = file.read()
keep.login('xxxxxxxx', pwd)
note = keep.createNote('title', 'text')
note.labels.add('calls')

Is giving me this error.

Traceback (most recent call last):
  File "C:/Users/Jason/Google Drive/pycharm/test.py", line 8, in <module>
    note.labels.add('calls')
  File "C:\Python27\lib\site-packages\gkeepapi\node.py", line 922, in add
    self._labels[label.id] = label
AttributeError: 'str' object has no attribute 'id'

Here is the docs. https://gkeepapi.readthedocs.io/en/latest/#manipulating-labels-on-notes. I think I'm doing the right thing, but I'm obviously not.


Solution

  • I would say just about every bug in the history of computers was caused by someone with the thought "I think I'm doing the right thing" in their head :-)

    More seriously, the label that you add to a note is meant to be label rather than a string. That's supported by the fact that it's trying to get your label identifier from a string object (which doesn't have one):

    'str' object has no attribute 'id'

    This is actually described in the link you provided, just three sections up from your anchor point:

    Creating Labels
         New labels can be created with Keep.createLabel():
              label = keep.createLabel('todo')

    : : blah blah blah

    Manipulating Labels on Notes
         When working with labels and notes, the key point to remember is that we’re always working with node.Label objects or IDs.

    Hence you can create a label and add it to the note with the lines:

    callLabel = keep.createLabel('calls')
    note.labels.add(callLabel)
    

    If you already have a label with that name, you can get it with:

    callLabel = keep.findLabel('calls')
    

    So, if you wanted to handle both possibilities seemlessly, try to create the label inside a try/catch and, if you get an exception, do the find.