2011-09-12 76 views
5

我使用下面的代碼:ASIHTTPRequest基本身份驗證失敗

NSString *params = [NSString stringWithFormat:@"{\"username\": \"%@\", \"password\": \"%@\", \"client_id\": \"%@\", \"client_secret\": \"%@\"}", 
        mFieldUsername.text, mFieldPassword.text, [NTDefaults clientId], [NTDefaults clientSecret]]; 
NSLog(@"Params:\n%@", params); 

NSURL *url = [NSURL URLWithString:[[NTDefaults baseEndpointUrl] stringByAppendingString:@"/api/token.json"]]; 
ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request addRequestHeader:@"Content-Type" value:@"application/json"]; 
[request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d",params.length]]; 
[request setAuthenticationScheme:(NSString *) kCFHTTPAuthenticationSchemeBasic]; 
[request setUsername:@"user"]; 
[request setPassword:@"pass"]; 
[request setShouldPresentCredentialsBeforeChallenge:YES]; 

[request setPostBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 

NSLog(@"Post Body: %@", request.postBody); 
NSLog(@"Headers: %@", request.requestHeaders); 
NSLog(@"URL: %@", url.absoluteString); 
[request setDelegate:self]; 
[request startAsynchronous]; 

這是返回:

錯誤:驗證需要

是否有人對此有什麼想法?成功使用ASIHTTPRequest進行身份驗證?

回答

7

我成功地使用:

request.shouldPresentCredentialsBeforeChallenge = YES; 
[request addBasicAuthenticationHeaderWithUsername:@"myUser" 
             andPassword:@"myPass"]; 

我不爲我的基本身份驗證使用setAuthenticationScheme。

+0

謝謝,我認爲這與帖子正文有關。我現在用[請求addPostValue:mFieldUsername.text forKey:@「username」]設置它;即使如此,你的解決方案也是有幫助的......絕對有效。謝謝! – Herm