【发布时间】:2013-02-06 20:15:11
【问题描述】:
我刚刚开始使用 RestKit,并且在 Rk 0.20 即将上线并且文档和演示落后了一步。网络上的大多数东西都是针对 RK 0.10 的,0.20 版本有很大的变化。
当新版本很快就会启动并运行时,我不想退回到早期版本。
我在 URL“test.myserver.com”上有一个 JSON 资源,它返回一个简单的数据报 - { “id_user”:“4401”, “数据位置”:“4401”, “国家”:“英国”, “数据”:“测试数据”, “登录”:“弗雷德博客”, “密码”:“579c0cb0ed2dc25db121283f7a98cc71”, “访问级别”:“2”, “时间戳”:“1012”, “数据哈希”:“2749da29f20ce7a85092323f193adee8” }
我很确定我已经对映射等进行了排序,但我的服务需要身份验证,所以我需要在请求中将用户名和密码传递给服务器。
到目前为止我已经得到了这个
NSURL *url = [NSURL URLWithString:@"http://test.myserver.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
这似乎联系了服务器,但不可避免地记录了以下错误
restkit.network:RKObjectRequestOperation.m:296 对象请求失败:底层 HTTP 请求操作失败并出现错误:Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 “预期状态代码在 (200-299),得到401" 用户信息=0x7884030 {NSLocalizedRecoverySuggestion={ “错误”: { “代码”:401, “消息”:“未经授权:需要身份验证” } }, AFNetworkingOperationFailingURLRequestErrorKey=http://elancovision.umfundi.com>, NSErrorFailingURLKey=http://elancovision.umfundi.com, NSLocalizedDescription=预期状态码在 (200-299), 得到 401, AFNetworkingOperationFailingURLResponseErrorKey=}
问题当然是我如何将用户名和密码添加到请求中。
抱歉这个菜鸟问题!
【问题讨论】:
-
您需要知道使用哪种协议对 API 进行身份验证。你能在你的问题中准确地说出来吗? RESTKit 的网络层依赖于 AFNetworking。因此,就身份验证而言,您将不得不处理这个库。
-
我能告诉你的最好的就是服务器端是使用restler库用PHP编写的。
-
使用'BasicAuthenticaton'
标签: restkit restkit-0.20