0

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?

4

3 回答 3

1

您没有正确传递身份验证令牌。这是您在 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);
    };
}];
}
于 2013-06-19T20:35:42.107 回答
0

403 - 表示禁止。我没有使用 Evernote 的经验,但是基于其他 SDK,你在请求​​方面做错了,你应该去 Evernote 开发者页面。登录到您的帐户并寻找一些触发器。可能有一些触发器您应该打开以使用此功能

于 2013-06-19T17:25:59.543 回答
-1

你应该:

  1. 确保您访问的是您打算访问的完整构建的 URL
  2. 检查整个返回的正文,看看是否提供了额外的错误信息

检查完整的 URL

中设置断点-[AFHTTPClient postPath: parameters: success: failure:]。在调试器中,键入po request以查看 AFNetworking 正在访问的完整 URL。确保这是你想要的。

检查整个身体

在您的故障块中,您只查看error由 AFNetworking 创建以总结问题的对象。但是,Evernote API 可能会在响应正文中提供其他信息,您可以使用NSLog(@"Response Body: %@", [operation responseString]).

概括

最终,您的 AFNetworking 代码很好 - 这看起来像是 Evernote API 问题。您提出的请求错误,您的令牌已过期,或者他们有错误。

旁注

  • 为每个请求创建一个新的 AFHTTPClient 实例效率很低。您可能应该使用单例模式来拥有一个在您的应用程序的整个生命周期中都存在的模式。
  • 你应该在之前做一些错误检查note.thumbnail = responseObject;responseObject可以是任何东西;在打电话给你的二传手之前,请确保这是你所期望的。
于 2013-06-19T17:27:15.210 回答