iOS Afnetworking未经授权(401)

时间:2014-08-01 10:19:58

标签: ios afnetworking afnetworking-2 unauthorized

我使用了afknetwordking 2.0。错误未授权401.我复制我的代码和错误。你能帮助我吗?,拜托。我不会解决这个问题

NSString *token = [PMUserDefaults getAccessToken];
NSString *finalyToken = [[NSString alloc]initWithFormat:@"Bearer %@",token ];
DLog(@"LIKESAAAA %@", finalyToken);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"Authorization" forHTTPHeaderField:finalyToken];
[manager.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];


NSDictionary *parameters = @{@"id": @"contentId"};
[manager POST:@"http://IP/Folder/api/Folder/parameter/content_like/parameter" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

"Cache-Control" = "no-cache";
    "Content-Length" = 61;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Fri, 01 Aug 2014 09:57:07 GMT";
    Expires = "-1";
    Pragma = "no-cache";
    Server = "Microsoft-IIS/8.0";
    "Www-Authenticate" = Bearer;
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
 NSLocalizedDescription=Request failed: unauthorized (401)

2 个答案:

答案 0 :(得分:3)

401状态为HTTP Error Unauthorized。 所以你可能在authen步骤中做错了。

您的代码可能会失败

[manager.requestSerializer setValue:@"Authorization" forHTTPHeaderField:finalyToken];

需要看起来像

[manager.requestSerializer setValue:finalyToken forHTTPHeaderField:@"Authorization"];

试试吧

答案 1 :(得分:0)

在SWIFT 2.0 for iOS9中,使用:

sessionManager.requestSerializer.setValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")