2011-09-12 12 views
5

estoy usando el siguiente código:ASIHTTPRequest autenticación básica no

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]; 

Qué está regresando:

Error: la autenticación necesaria

¿Alguien tiene alguna idea sobre esto? ¿Se ha autenticado con éxito usando ASIHTTPRequest?

Respuesta

7

utilizo con éxito:

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

No consumo setAuthenticationScheme para mi la autenticación básica.

+0

Gracias, creo que esto tenía más que ver con el cuerpo del mensaje. Ahora lo estoy configurando con [request addPostValue: mFieldUsername.text forKey: @ "username"]; etc. Aun así, tu solución también fue útil ... definitivamente funcionó. ¡Gracias! – Herm

Cuestiones relacionadas