2017-02-04 4 views
0

Ich schreibe eine Anwendung, um sich in Spotify zu autorisieren. Früher habe ich die Knoten app.js Beispiel Dinge zum Laufen zu bringen und ein jetzt neu schreiben nativ in Objective C. ich den Autorisierungscode über eine Callback-FunktionFehler 400 und 415 beim Austausch von Code für access_token

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://accounts.spotify.com/api/token"]]; 
request.HTTPMethod = @"POST"; 


// put in the header fields 
NSString *headerString = [NSString stringWithFormat:@"%@:%@",clientID,clientSecret]; 
NSData *nsdata = [headerString dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0]; 
base64Encoded = [ NSString stringWithFormat:@"Basic %@", base64Encoded]; 
[request setValue:base64Encoded forHTTPHeaderField:@"Authorization"]; 
NSLog(@"request.header %@", request.allHTTPHeaderFields.description); 

// put in the body form 
NSString * stringData = [NSString stringWithFormat:@"grant_type:%@, code:%@, redirect_uri:\"%@\"", @"authorization_code",authorisationCode,redirectUri]; 
NSData * requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding]; 
request.HTTPBody = requestBodyData; 
NSLog(@"request.body %@", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]); 

extrahiert habe ich dann diese Anfrage posten und bekommen {“ error ":" server_error "," error_description ":" Unerwarteter Status: 415 "} zurückgegeben.

Ich habe eine Vielzahl von Fragen zu diesem Thema rund um den Content-Type gefunden, die application/x-www-form-urlencoded sein müssen, aber das war keine direkte Heilung.

Wer hat irgendwelche Vorschläge für mich?

+0

I sollte sagen, dass ich mit Postman bewiesen habe, dass die Base64-Codierung korrekt ist und dass die Verwendung von 'application/x-www-form-urlencoded' korrekt funktioniert und mir einen access_code liefert –

Antwort

0

fand ich die Antwort hier NSURLRequest : Post data and read the posted page

Es scheint, dass die Rohdaten im HTTPBody unterschiedlich formatiert werden muss - überhaupt keine JSON. Der obige Link hat eine nützliche Funktion, die ich

leicht

aktualisiert - (NSData *) encodeDictionary: (NSDictionary *) Wörterbuch {

NSMutableArray *parts = [[NSMutableArray alloc] init]; 
for (NSString *key in dictionary) { 

    NSString *encodedValue = [[dictionary objectForKey:key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPasswordAllowedCharacterSet]]; 
    NSString *encodedKey = [key stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPasswordAllowedCharacterSet]]; 

    NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue]; 
    [parts addObject:part]; 
} 

NSString *encodedDictionary = [parts componentsJoinedByString:@"&"]; 
return [encodedDictionary dataUsingEncoding:NSUTF8StringEncoding]; 

}

und ..

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
//NSLog(@"request.header %@", request.allHTTPHeaderFields.description); 

// put in the body form using the new function 
NSDictionary * parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"authorization_code",@"grant_type",authorisationCode,@"code",redirectUri,@"redirect_uri", nil ]; 
NSData * requestBodyData = [self encodeDictionary:parameters]; 
request.HTTPBody = requestBodyData; 
Verwandte Themen