sirven sirve que por para pagina las keywords etiquetas ejemplos cursiva cuantos ios curl http-headers nsurlconnection get-request

ios - sirve - El encabezado de autorización de NSURLConnection no funciona



tags h1 ejemplos (2)

Estoy tratando de enviar un token de acceso OAuth en un encabezado HTTP a través de NSURLConnection, pero no parece estar enviando el encabezado porque la API me sigue dando un error que dice que "debe proporcionar el token de autorización".

Este es el código que estoy usando:

NSURL *aUrl = [NSURL URLWithString: @"http://generericfakeapi.com/user/profile"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request addValue:[NSString stringWithFormat:@"OAuth %@", token] forHTTPHeaderField:@"Authorization"]; [request setHTTPMethod:@"GET"]; NSError *error = nil; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error: &error]; NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error]; NSLog(@"Response : %@", JSONDictionary);

Y este es un ejemplo del comando cURL para la API:

curl ''http://generericfakeapi.com/user/profile'' -H ''Authorization: OAuth YourAuthToken''

¿No es esto lo que esencialmente estoy haciendo a través de NSURLConnection?

Cualquier ayuda sería apreciada.


Cambia esta línea:

NSURL *aUrl = [NSURL URLWithString: @"http://generericfakeapi.com/user/profile"];

A:

NSURL *aUrl = [NSURL URLWithString: @"http://generericfakeapi.com/user/profile/"];

Aparentemente, iOS deja caer el encabezado de Autorización si no hay una barra al final de una URL. Este problema me costó literalmente el sueño por dos días.


Para mi se ve bien. ¿Estás seguro de que has dado un token válido? Intenta atrapar el error como este

if (error) { NSLog(@"error : %@", error.description); }

Mi código funciona bien:

NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://....ID=%i", cellID]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:jsonURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0]; [request setValue:@"Basic ...." forHTTPHeaderField:@"Authorization"]; NSURLResponse *response; NSError * error = nil; NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Espero eso ayude