ios - AFNetworking,使用正确的 token 获取 401

标签 ios rest afnetworking

我在 https://www.ez-point.com/api/v1/ezpoints 上提出请求使用 Chrome Rest Client 使用适当的 token 。我正在正确地得到结果。但是,在使用 AFNetworking 时,我收到了 401。

这是我的代码片段:

NSURL *url = [[NSURL alloc] initWithString:@"https://www.ez-point.com/api/v1/"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url ];
[httpClient setAuthorizationHeaderWithToken:@"xxxxxxxxxx"];

NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:@"/ezpoints" parameters:nil];

AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"%@", @"success");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"%@", @"Error");

这里是错误:

2013-10-29 08:51:38.908 EZ-POINT[4944:c07] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-29 08:51:39.189 EZ-POINT[4944:c07] I restkit.network:RKObjectRequestOperation.m:180 GET 'https://www.ez-point.com/ezpoints'
2013-10-29 08:51:41.908 EZ-POINT[4944:c07] E restkit.network:RKObjectRequestOperation.m:209 GET 'https://www.ez-point.com/ezpoints' (401 Unauthorized) [2.7191 s]: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x1052ef40 {AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://www.ez-point.com/ezpoints>, NSErrorFailingURLKey=https://www.ez-point.com/ezpoints, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xac54520>}
2013-10-29 08:51:41.908 EZ-POINT[4944:c07] Error

最佳答案

它使用以下代码:

NSURL *url = [[NSURL alloc] initWithString:@"https://www.ez-point.com/api/v1/ezpoints"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setValue:@"4f62fab9c91c46ad971cc2ae4a32bb6f" forHTTPHeaderField:@"Authorization"];
    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSLog(@"%@",[JSON objectForKey:@"status"]);
        }
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                NSLog(@"%@", @"Error");
        }];
    [operation start];

关于ios - AFNetworking,使用正确的 token 获取 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19649986/

相关文章:

iOS - 以编程方式更改约束关系

iphone - 无法使用 UILongTouchGestureRecognizer 从 MKMapView 获取坐标

ios - 下载图像 编辑图像然后缓存

java - JQuery 验证 - 如何在 "remote"方法中正确检索表单字段值?

django - 使用 token 身份验证时如何在 django channel 中的 websocket 连接中对用户进行身份验证

ios - JSON AFNetworking iOS 6 查看值是否为 "on"

ios - 使用 AFNetworking 2 和可达性没有互联网连接警报

ios - 什么是 "dequeue cell"

ios - 在iOS7中使用CGContext绘图时出现问题

java - 强制返回 JSON 的 Java Jersey RESTful Web 服务出现 HTTP 404 错误