"Connect to Evernote"

Sun 14 February 2010

#import "THTTPClient.h"

#import "TBinaryProtocol.h"

#import "UserStore.h"

#import "NoteStore.h"

 

 

- (void)Test

{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Keep this key private

NSString *consumerKey = [[[NSString alloc]

  initWithString: @"YOUR_CONSUMER_KEY_HERE" ] autorelease];

NSString *consumerSecret = [[[NSString alloc]

initWithString: @"YOUR_CONSUMER_SECRET_HERE"] autorelease];

// For testing we use the sandbox server.

NSURL *userStoreUri = [[[NSURL alloc]

initWithString: @"https://sandbox.evernote.com/edam/user"] autorelease];

NSString *noteStoreUriBase = [[[NSString alloc]

  initWithString: @"http://sandbox.evernote.com/edam/note/"] autorelease];

// These are for test purposes. At some point the user will provide his/her own.

NSString *username = [[[NSString alloc]

  initWithString: @"YOUR_USERNAME_HERE"] autorelease];

NSString *password = [[[NSString alloc]

  initWithString: @"YOUR_PASSWORD_HERE"] autorelease];

THTTPClient *userStoreHttpClient = [[[THTTPClient alloc]

initWithURL:userStoreUri] autorelease];

TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc]

  initWithTransport:userStoreHttpClient] autorelease];

EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc]

  initWithProtocol:userStoreProtocol] autorelease];

EDAMNotebook* defaultNotebook = NULL;

BOOL versionOk = [userStore checkVersion:@"Cocoa EDAMTest" :

  [EDAMUserStoreConstants EDAM_VERSION_MAJOR] :

  [EDAMUserStoreConstants EDAM_VERSION_MINOR]];

if (versionOk == YES)

{

EDAMAuthenticationResult* authResult =

[userStore authenticate:username :password

  :consumerKey :consumerSecret];

EDAMUser *user = [authResult user];

NSString *authToken = [authResult authenticationToken];

NSLog(@"Authentication was successful for: %@", [user username]);

NSLog(@"Authentication token: %@", authToken);

NSURL *noteStoreUri =  [[[NSURL alloc]

initWithString:[NSString stringWithFormat:@"%@%@",

noteStoreUriBase, [user shardId]] ]autorelease];

THTTPClient *noteStoreHttpClient = [[[THTTPClient alloc]

initWithURL:noteStoreUri] autorelease];

TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc]

  initWithTransport:noteStoreHttpClient] autorelease];

EDAMNoteStoreClient *noteStore = [[[EDAMNoteStoreClient alloc]

  initWithProtocol:noteStoreProtocol] autorelease];

NSArray *notebooks = [[noteStore listNotebooks:authToken] autorelease];

NSLog(@"Found %d notebooks", [notebooks count]);

for (int i = 0; i < [notebooks count]; i++)

{

EDAMNotebook* notebook = (EDAMNotebook*)[notebooks objectAtIndex:i];

if ([notebook defaultNotebook] == YES)

{

defaultNotebook = notebook;

}

NSLog(@" * %@", [notebook name]);

}

NSLog(@"Creating a new note in default notebook: %@", [defaultNotebook name]);

// Skipping the image resource section...

EDAMNote *note = [[[EDAMNote alloc] init] autorelease];

[note setNotebookGuid:[defaultNotebook guid]];

[note setTitle:@"Test note from Cocoa Test."];

NSMutableString* contentString = [[[NSMutableString alloc] init] autorelease];

[contentString setString: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];

[contentString appendString:@"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml.dtd\">"];

[contentString appendString:@" <en-note>Here is the Olive Tree Test note.<br/>"];

[contentString appendString:@" </en-note>"];

[note setContent:contentString];

[note setCreated:(long long)[[NSDate date] timeIntervalSince1970] * 1000];

EDAMNote *createdNote = [noteStore createNote:authToken :note];

if (createdNote != NULL)

{

NSLog(@"Created note: %@", [createdNote title]);

}

}

[pool drain];

}