我在我的项目中使用了AFNetworking,每当我发送请求时,在失败的块中会收到以下消息:
错误域=com.alamofire.error.serialization.response Code=-1011 "请求失败:未经授权(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=请求失败:未经授权(401)}
这是我使用AFNetworking发送请求的方式。
但如果我能够通过使用NSURLSession获得正确的响应。(请参考下面的代码)
我对使用AFNetworking很感兴趣,但由于这个问题,我无法继续前进。请帮助我纠正AFNetworking中的问题。
这是我应该发送的请求:
这是我应该得到的回复,
错误域=com.alamofire.error.serialization.response Code=-1011 "请求失败:未经授权(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=请求失败:未经授权(401)}
这是我使用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);
}
}];
}
但如果我能够通过使用NSURLSession获得正确的响应。(请参考下面的代码)
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];
我对使用AFNetworking很感兴趣,但由于这个问题,我无法继续前进。请帮助我纠正AFNetworking中的问题。
这是我应该发送的请求:
{
"user": {
"uid": "102364120025511",
"access_token": "g6tdbvc34seadcx7yufbbcgaavf8ijh",
"email": "user@domain.com",
"latitude": 6.927079,
"longitude": 79.861243,
"remote_avatar_url": ""
}
}
这是我应该得到的回复,
{
"success": true,
"message": "Successfully authenticated",
"user": {
"id": 123,
"email": "user@domain.com",
"provider": "facebook",
"latitude": 6.927079,
"longitude": 79.861243,
"avatar": {
"url": "",
"thumb": ""
}
}
}
7b227375...
的数据包含了什么,那就是一个JSON负载,表示“{"success":false,"message":"Authentication failed}"。 - Rob