Question

I am developing an iOS app that uses the Evernote API. Everything has been working fine, but then I started getting "code 403" to my requests.

Authentication to the service goes well: I am able log on and download all info I need (Notebooks, Notes, Note content, etc...). But when I try to get the thumbnails, I get 403.

My code for the request:

NSString *thumbnailPath = [NSString stringWithFormat:@"%@thm/note/%@?75", [[EvernoteSession sharedSession] webApiUrlPrefix], note.guid];
NSLog(@"THUMBNAILPATH %@", thumbnailPath);

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:[[EvernoteSession sharedSession] webApiUrlPrefix]]];
[httpClient clearAuthorizationHeader];
[httpClient setAuthorizationHeaderWithToken:[[EvernoteSession sharedSession] authenticationToken]];

[httpClient postPath:thumbnailPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

    note.thumbnail = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"REQUEST: %@", thumbnailPath);
    NSLog(@"Error: %@", error.localizedDescription);
}];

If I copy what the "REQUEST:" log result, it is a well-formatted link that gives me the thumbnail in my browser. But the second log gives me: "Error: Expected status code in (200-299), got 403".

I am out of ideas. Can anyone help?

Was it helpful?

Solution

You are not passing in the auth token correctly. This is how you would request a thumbnail for a note on iOS :

- (void)getThumbnailWithNoteGuid:(NSString*)noteGUID {
    EvernoteSession* session = [EvernoteSession sharedSession];
    NSString* fullTumbnailURL = [NSString stringWithFormat:@"%@thm/note/%@",[[EvernoteSession sharedSession]webApiUrlPrefix],noteGUID];
    NSURL *url = [NSURL URLWithString:fullTumbnailURL];
    NSMutableURLRequest* urlReq = [NSMutableURLRequest requestWithURL:url];
    [urlReq setHTTPMethod:@"POST"];
    [urlReq setHTTPBody:[[NSString stringWithFormat:@"auth=%@",[session.authenticationToken URLEncodedString]] dataUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"full URL %@",fullTumbnailURL);
    [NSURLConnection sendAsynchronousRequest:urlReq queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *urlREsp, NSData *respData, NSError *error) {
    if(error == nil) {
        NSLog(@"Thumbail data : %@",respData);
    };
}];
}

OTHER TIPS

403 - means forbidden. I haven't got expirience working with evernote but in based on other SDK's you're doing something wrong with request either you should go to evernote developers page. log in to your's account and look for some triggers. may be there some triggers which you should turn on to use this features

You should:

  1. Make sure that you're hitting the fully constructed URL you intend to
  2. Examine the entire returned body to see if there is additional error info provided

Examine full URL

Set a breakpoint in -[AFHTTPClient postPath: parameters: success: failure:]. In the debugger, type po request to see the full URL that AFNetworking is hitting. Make sure that's what you want.

Examine entire body

In your failure block, you're only looking at the error object, created by AFNetworking to summarize the issue. However, the Evernote API could be providing additional info in the response body, which you can look at with NSLog(@"Response Body: %@", [operation responseString]).

Summary

Ultimately, your AFNetworking code is fine - this looks like an Evernote API issue. You're making the request wrong, your token is expired, or there's a bug on their end.

Side notes

  • It's inefficient to create a new AFHTTPClient instance for every request. You should probably use the singleton pattern to have one that sticks around for the lifetime of your app.
  • You should do some error checking before note.thumbnail = responseObject;. responseObject could be anything; make sure it's what you expect before you call your setter.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top