2012-12-11 109 views
2

是否有可能在具有AFNetworking的相同请求中使用Basic和OAuth授权标头(避免覆盖)?基本和OAuth身份验证标头

我有这样的代码:

NSURL *url = [NSURL URLWithString:@"https://www.infojobs.net/"]; 
AFOAuth2Client *OAuthClient = [[AFOAuth2Client alloc] initWithBaseURL:url clientID:kClientID secret:kClientSecret]; 

[OAuthClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

[OAuthClient authenticateUsingOAuthWithPath:@"oauth/authorize" code:self.authorizationCode redirectURI:kInfoJobsRedirectURLString success:^(AFOAuthCredential *credential) { 
    NSLog(@"Credentials: %@", credential.accessToken); 
    if (![credential.accessToken isEqualToString:@""]) { 
     self.isAuthenticated = YES; 

     [AFOAuthCredential storeCredential:credential withIdentifier:@"kInfoJobsAccessToken"]; 


     [[InfoJobsAPI sharedClient] setAuthorizationHeaderWithToken:credential.accessToken]; 

     // (!) This overwrites the Authorization header set with the accessToken 
     [[InfoJobsAPI sharedClient] setAuthorizationHeaderWithUsername:kClientID password:kClientSecret]; 

     success(credential); 

    } 
} failure:^(NSError *error) { 
    NSLog(@"Error: %@", error.localizedDescription); 

}]; 

我需要这样的请求:

GET /api/1/application HTTP/1.1 
Host: api.infojobs.net 
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 
Authorization: OAuth 07d18fac-77ea-461f-9bfe-a5e9d98deb3d 
.... 

但我不能设置“基本”,并在“OAuth的”授权头相同的请求,因为AFNetworking似乎覆盖了此头文件,如documentation

在同一个授权hea中可以使用“Basic”和“OAuth” der,也许是用“\ n”分割?

谢谢,我的英文不好


编辑对不起

最后,我可以用“基本”,并在相同的标题“的Oauth”认证,这是代码:

[[InfoJobsAPI sharedClient] setAuthorizationHeaderWithUsername:kClientID password:kClientSecret]; 

AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:@"kInfoJobsAccessToken"]; 

NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:@"/api/2/candidate" parameters:nil]; 

[request addValue:[NSString stringWithFormat:@"OAuth %@", credential.accessToken] forHTTPHeaderField:@"Authorization"]; 

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

[operation start]; 
+0

我很好奇你的解决方案如何工作。通过将“OAuth xxx(token)xxx”作为附加值添加到您的授权标头中,您与之通信的任何服务器都知道要检查Auth标头的哪一部分? –

回答

1

根据HTTP规范,请求中只能有一个Authorization标头。因此,根据该规范,图书馆显示的行为是正确的:第二次拨打setAuthorizationHeader...会覆盖前一个。

你通常会在HTTP中看到的是握手阶段,服务器告诉客户端它可以接受哪些授权协议。然后客户可以从这些协议中进行选择,使用哪一个协议。