Skip to content

Using Python package gkeepapi to access Google Keep

Last Updated on 2020-12-17 by Clay


Introduction

Today I found a third-party Python package gkeepapi about Google Keep that looks very convenient and is not officially authorized.

The package allows us to read the content of Google Keep of our account, and also allows us to automatically upload text, notes and others things to Google Keep.

If you are interested in this useful gadget, please check it out on the developer's Github: https://github.com/kiwiz/gkeepapi

The developer has a reminder: This API is not officially authorized and supported by Google and is still under development. Please do not apply it to commercial products.


What is Google Keep

Google Keep is an online note-taking service released by Google in 2013. We can edit the same document together on mobile phones, computer desktop environments, web pages, etc. via the Internet. (Note: This is based on Google Drive)

This tool is very convenient for people who need to record at any time, whether it is writing a novel, or suddenly wanting to record notes, writing inspiration-it is very easy to use, not to mention that it can be directly read on other platforms afterwards, continue Edit down.


How to use gkeepapi

I personally think that the advantage of gkeepapi is that it can "read existing notes" and "post new notes", so we also start from these two points.

If you want to further explain the operation, maybe you can refer to this website: https://gkeepapi.readthedocs.io/en/latest/#manipulating-notes

如果是第一次使用,要使用以下指令下載:

pip3 install gkeepapi

After the installation is over, let's look at a simple example.

# -*- coding: utf-8 -*-
import json
import gkeepapi

with open('Account.json', 'r', encoding='utf-8') as f:
    account = json.load(f)

keep = gkeepapi.Keep()


In order not to disclose the account password, here I read my account password in Json format.

try:
    success = keep.login(account['UserName'], account['Password'])
except:
    print('Maybe you need to go to this website to enable permissions: https://accounts.google.com/b/0/DisplayUnlockCaptcha')
    raise


Here is to log in with keep and use Google account password. If it appears:

gkeepapi.exception.LoginException: ('NeedsBrowser', 'To access your account, you must sign in on the web. Touch Next to start browser sign-in.')

For the above error, you can go to https://accounts.google.com/b/0/DisplayUnlockCaptcha to give authorization.


Upload notes

First of all, let's test the function of some notes.

# Create Note
note = keep.createNote('TEST', 'Today is a nice day.')
note.pinned = True
note.color = gkeepapi.node.ColorValue.Brown
keep.sync()


The code is very simple, that is, the note with the title "TEST" and the content "Today is a nice day." is sent to your Google Keep.

It means:

note.color = gkeepapi.node.ColorValue.Brown


Give your keep note a color.

After a while, the note has been uploaded to Google Keep.


Load notes

So next, let's read the content.

# READ
notes = keep.all()
for note in notes:
    if note.title == 'TEST':
        print([note.title, note.text])


Output:

['TEST', 'Today is a nice day.']

First, we use keep.all() to read all notes, and then we can use note.title and note.text to view the title and content of the note.


change note content

With a slight modification based on the code just now, we can complete the program to "change" the content of the note.

notes = keep.all()
for note in notes:
    if note.title == 'TEST':
        note.title = 'TEST 2'
        note.text = 'Today is not a nice day.'
        note.color = gkeepapi.node.ColorValue.White
        note.pinned = True

keep.sync()


Output:

This is the note titled "TEST" I just created. Now I have made some changes to the note content and changed the note color to white.


Delete notes

Finally, let's briefly talk about how to delete the notes.

notes = keep.all()
for note in notes:
    if note.title == 'TEST 2':
        note.delete()

keep.sync()


In this way, I deleted the TEST 2 I just created.

2 thoughts on “Using Python package gkeepapi to access Google Keep”

  1. when we run the code the following error occurred what will be the value of account.json.

    with open(‘Account.json’, ‘r’, encoding=’utf-8′) as f:
    FileNotFoundError: [Errno 2] No such file or directory: ‘Account.json’

Leave a Reply