Radio TukTuk Radio TukTuk - 5 months ago 9
Ruby Question

Not able to create notebook and transfer files in evernote

I am developing a web application and I need to create an EverNote notebook and transfer some files in it.

I am able to authenticate user from EverNote but not able to create notebook. I'm also confused how to transfer files in that notebook.

Here is the authentication code. API URL

For creating a notebook:

noteStore.createNotebook(access_token.token, "my_notebook")


Error:

An error occurred: undefined method `write' for "my_notebook":String


edit

following seth's lead

notebook = Evernote::EDAM::Type::Notebook.new()
notebook.name = "my_notebook3"
x= noteStore.createNotebook(access_token.token, notebook)
note = Evernote::EDAM::Type::Note.new()
note.notebookGuid = x.guid
note.title="my note"
y=noteStore.createNote(access_token.token,note)


working on file transfer in that note.

Answer

The second parameter is a Notebook structure, not a string. You need to do something like:

notebook = Evernote::EDAM::Type::Notebook.new()
notebook.name = "my_notebook"
noteStore.createNotebook(access_token.token, notebook)

Evernote notebooks contain only Notes, and Notes can have files attached to them. To attach a file to a new note, you need to create a Resource and include it in the note:

filename = # the file that you want to attach
image = File.open(filename, "rb") { |io| io.read }
hashFunc = Digest::MD5.new
hashHex = hashFunc.hexdigest(image)

data = Evernote::EDAM::Type::Data.new()
data.size = image.size
data.bodyHash = hashHex
data.body = image

resource = Evernote::EDAM::Type::Resource.new()
resource.mime = # the appropriate MIME type
resource.data = data
resource.attributes = Evernote::EDAM::Type::ResourceAttributes.new()
resource.attributes.fileName = filename

note = Evernote::EDAM::Type::Note.new()
note.title = "Title"
note.content = '<?xml version="1.0" encoding="UTF-8"?>' +
  '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' +
  '<en-note>' +
  '<en-media type="' + resource.mime + '" hash="' + hashHex + '"/>' +
  '</en-note>'
note.resources = [ resource ]

createdNote = noteStore.createNote(authToken, note)

The sample code in the Evernote API ZIP file demonstrates this. You can download the ZIP from http://www.evernote.com/about/developer/api/.

Comments