2013-10-29 40 views
0

我正在使用Chrome Rest Client以正確的令牌對https://www.ez-point.com/api/v1/ezpoints發出請求。我正確地得到結果。然而,在使用時AFNetworking,我得到一個401AFNetworking,使用合適的令牌獲得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 

回答

1

及其與下面的代碼工作:

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