Evernote API : Create notes in a linked notebook

This time, I'm going to show you how to create notes in a linked notebook.

Here's the context :

User A shares a notebook with User B and gives her full permissions on it. That means User B can read but also create new notes in the shared (linked) notebook.

There's actually a difference between a shared and a linked notebook :

In our case, the shared notebook is from A's perspective (i.e. the user who shares the notebook), whereas the linked notebook is from B's perpective (i.e. the user who gets a link to a notebook). For more (and better) explanations, see the official doc.

So, what we want to achieve is to create notes in the linked notebook with B's account through the API.

Here's how things work :

First you need to get access to the notebook, i.e. authenticate to the shared notebook to get a auth token that will allow you to modify the notebook.


# get linked notebooks
$linked_notebooks = $noteStore->listLinkedNotebooks($authToken);
$linked_notebook = $linked_notebooks[0];

# shareKey for the first notebook
$shareKey = $linked_notebook->shareKey;

# get the right noteStore
$note_store_uri = $linked_notebook->noteStoreUrl;
$shared_note_store = $client->getSharedNoteStore($linked_notebook);

# authenticate to the linked notebook
$auth_result = $shared_note_store->authenticateToSharedNotebook($shareKey, $authToken);

# get the share token
$share_token = $auth_result->authenticationToken;

Now that you have the token, you need to retrieve the shared notebook to get its guid :

# get the shared notebook
$sharedNotebook = $shared_note_store->getSharedNotebookByAuth($share_token);

$guid = $sharedNotebook->notebookGuid;

The trick here is to retrieve the guid from the shared notebook. A common error would be to use the guid attribute of the linked notebook. This would result in a EDAMNotFoundException as the linked notebook guid is not the same as the one of the shared notebook.

The last step is to create the note. Be careful to use the right notestore (i.e. not yours but the "shared" one) with the right token (again : not yours but the one you retrieved from the authenticateToSharedNotebook() method).

$note = new Note();
$note->notebookGuid = $guid;
$note->title = 'It Works';

$shared_note_store->createNote($share_token, $note);

That's it !

Comment on this post