iOS上Evernote API的HTTP请求:错误代码403



我正在开发一个使用Evernote API的iOS应用程序。一切都很好,但后来我开始得到"代码403"我的请求。

服务的认证进展顺利:我可以登录并下载我需要的所有信息(笔记本,笔记,笔记内容等)。但是当我尝试获取缩略图时,我得到403。

请求的代码:

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);
}];

如果我复制"REQUEST:"日志结果,它是一个格式良好的链接,在我的浏览器中给出缩略图。但是第二个日志告诉我:"错误:预期状态码在(200-299)中,得到403"。

我没有主意了。有人能帮忙吗?

您没有正确地传递验证令牌。这是你在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);
    };
}];
}

403 -表示禁止。我没有经验与evernote工作,但在基于其他SDK的你做了一些错误的请求要么你应该去evernote开发者页面。登录你的账户,寻找一些触发因素。也许你应该打开一些触发器来使用这些功能

你应该:

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

查看完整URL

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

检查全身

在失败块中,您只查看由AFNetworking创建的error对象,以总结问题。但是,Evernote API可以在响应体中提供额外的信息,您可以查看NSLog(@"Response Body: %@", [operation responseString])

总结

最终,你的AFNetworking代码是好的-这看起来像一个Evernote API问题。您发出的请求是错误的,您的令牌过期了,或者令牌端有错误。

边注
    为每个请求创建一个新的AFHTTPClient实例是低效的。你可能应该使用单例模式,让它在你的应用生命周期中一直存在。
  • 你应该在note.thumbnail = responseObject;之前做一些错误检查。responseObject可以是任何东西;在调用setter之前,确保它是你所期望的。

最新更新