Back

Step by Step Guide to Building an Evernote API Integration in Go

Aug 12, 20246 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to supercharge your note-taking game? Let's dive into the world of Evernote API integration using Go. We'll be using the awesome evernote-golang-sdk package to make our lives easier. Buckle up, because by the end of this guide, you'll be syncing notes like a pro!

Prerequisites

Before we jump in, make sure you've got:

  • Go installed on your machine (I know you do, you rockstar!)
  • An Evernote Developer account (grab one here if you haven't already)
  • Your API key and secret (keep these safe!)

Setting up the project

Let's get the boring stuff out of the way:

mkdir evernote-go-integration cd evernote-go-integration go mod init evernote-integration go get github.com/dreampuf/evernote-sdk-golang

Boom! You're all set up.

Authentication

Alright, time for the OAuth dance. Don't worry, it's not as complicated as it sounds:

import ( "github.com/dreampuf/evernote-sdk-golang/edam" "github.com/dreampuf/evernote-sdk-golang/evernote" ) client := evernote.NewClient(apiKey, apiSecret, evernote.SANDBOX) // Use PRODUCTION for real apps requestToken, requestSecret, err := client.GetRequestToken("http://your-callback-url") // Handle err authorizationURL := client.GetAuthorizeURL(requestToken) // Redirect user to authorizationURL and get the verifier accessToken, err := client.GetAccessToken(requestToken, requestSecret, verifier) // Handle err and store accessToken securely

Basic API Operations

Now that we're authenticated, let's get to the fun part:

userStore := client.GetUserStore() user, err := userStore.GetUser(accessToken) fmt.Printf("Hello, %s!\n", user.Username)

Working with Notes

Creating, fetching, updating, and deleting notes is a breeze:

noteStore := client.GetNoteStore() // Create a note note := &edam.Note{ Title: "My First Go Note", Content: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" + "<en-note>Hello from Go!</en-note>", } createdNote, err := noteStore.CreateNote(accessToken, note) // Fetch a note fetchedNote, err := noteStore.GetNote(accessToken, createdNote.GUID, true, false, false, false) // Update a note fetchedNote.Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" + "<en-note>Updated from Go!</en-note>" updatedNote, err := noteStore.UpdateNote(accessToken, fetchedNote) // Delete a note err = noteStore.DeleteNote(accessToken, createdNote.GUID)

Working with Notebooks

Manage your notebooks like a boss:

// List notebooks notebooks, err := noteStore.ListNotebooks(accessToken) // Create a notebook notebook := &edam.Notebook{ Name: "Go Notebook", } createdNotebook, err := noteStore.CreateNotebook(accessToken, notebook) // Update a notebook createdNotebook.Name = "Awesome Go Notebook" updatedNotebook, err := noteStore.UpdateNotebook(accessToken, createdNotebook)

Advanced Features

Let's kick it up a notch:

// Search for notes filter := &edam.NoteFilter{ Words: "Go", } spec := &edam.NotesMetadataResultSpec{} searchResults, err := noteStore.FindNotesMetadata(accessToken, filter, 0, 10, spec) // Work with tags tag := &edam.Tag{ Name: "golang", } createdTag, err := noteStore.CreateTag(accessToken, tag) // Handle attachments resource := &edam.Resource{ // Set up your resource here } noteWithAttachment := &edam.Note{ Title: "Note with Attachment", Content: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" + "<en-note>Check out this attachment!</en-note>", Resources: []*edam.Resource{resource}, } createdNoteWithAttachment, err := noteStore.CreateNote(accessToken, noteWithAttachment)

Error Handling and Best Practices

Always check for errors and handle them gracefully. Keep an eye on rate limits too:

if err != nil { if rateLimitError, ok := err.(*edam.EDAMSystemException); ok && rateLimitError.ErrorCode == edam.EDAMErrorCode_RATE_LIMIT_REACHED { // Handle rate limiting time.Sleep(time.Duration(rateLimitError.RateLimitDuration) * time.Second) } else { // Handle other errors log.Printf("Error: %v", err) } }

Conclusion

And there you have it! You're now an Evernote API integration wizard. Remember, this is just scratching the surface. There's so much more you can do with the Evernote API and Go.

Keep exploring, keep coding, and most importantly, keep having fun! If you want to dive deeper, check out the official Evernote API documentation and the evernote-golang-sdk GitHub repo.

Now go forth and build something awesome! 🚀