AFNetworking problem with answer: unauthorized (401)

I used AFNetworking in my project, whenever I sent a request, I get this message in the bounce block,

Domain error = com.alamofire.error.serialization.response Code = -1011 "Request failed: unauthorized (401)" UserInfo = {com.alamofire.serialization.response.error.response = com.alamofire.serialization.response. error.data = <7b227375 63636573 73223a66 616c7365 2c226d65 73736167 65223a22 41757468 656e7469 63617469 6f6e2066 61696c65 64227d>, NSLocalizedDescription = Request failed: unauthorized (401)}

This is how I submit a request using AFNetworking.

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil]; NSString *path = [NSString stringWithFormat:@"/api/##/##/##/sign_in"]; NSString *params = @"{\"user\": {\"uid\": \"10236412002551\",\"access_token\":\"g6tdbvc34seadcx7yufbbcgvf8ijhss\",\"email\": \" user@domain1.com \",\"latitude\": \"6.927079\", \"longitude\": \"79.861243\",\"remote_avatar_url\": \"\"}}"; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate]; [policy setValidatesDomainName:NO]; [policy setAllowInvalidCertificates:YES]; [manager POST:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) { if([[responseObject objectForKey:@"error_code"] intValue]==0){ if (success) { success(userArray); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } 

But if I can get the correct answer using NSURLSession (see code below)

  NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; NSURL * url = [NSURL URLWithString:@"http://##/api/##/##/##/sign_in"]; NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url]; NSString * params =@ "{\"user\": {\"uid\": \"10236412002551\",\"access_token\":\"g6tdbvc34seadcx7yufbbcgvf8ijhss\",\"email\": \" user@domain1.com \",\"latitude\": 6.927079, \"longitude\": 79.861243,\"remote_avatar_url\": \"\"}}"; [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"Response:%@ %@\n", response, error); if(error == nil) { NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"Data = %@",text); NSLog(@"Data = %@",json); } }]; [dataTask resume]; 

I am interested in using AFNetworking, but due to this problem I will not be able to move forward. Please help me fix a problem with AFNetworking.

This is the request I have to send,

 { "user": { "uid": "102364120025511", "access_token": "g6tdbvc34seadcx7yufbbcgaavf8ijh", "email": " user@domain.com ", "latitude": 6.927079, "longitude": 79.861243, "remote_avatar_url": "" } } 

This is the answer I should get

 { "success": true, "message": "Successfully authenticated", "user": { "id": 123, "email": " user@domain.com ", "provider": "facebook", "latitude": 6.927079, "longitude": 79.861243, "avatar": { "url": "", "thumb": "" } } } 
+5
source share
1 answer

You provide params as a JSON string, and then instruct AFNetworking to use AFJSONRequestSerializer to then represent that string in a JSON structure. The net effect is that you executed JSONify twice.

You must provide a dictionary object and let AFNetworking create JSON for you, for example:

 NSString *params = @{@"user": @{@"uid": @"10236412002551", @"access_token": @"g6tdbvc34seadcx7yufbbcgvf8ijhss", @"email": @" user@domain1.com ", @"latitude": @6.927079, @"longitude": @79.861243, @"remote_avatar_url": @""}}; 

By the way, by letting AFNetworking (which uses NSJSONSerialization ) build JSON, you will be much more reliable than trying to do it yourself. Therefore, even if you are using the NSURLSession approach, I would suggest creating this nested dictionary structure and then using NSJSONSerialization to create the JSON data payload. Or, if you use AFNetworking, he will do it for you.

+1
source

Source: https://habr.com/ru/post/1259373/


All Articles