【问题标题】:Error 400 and 415 when exchanging code for access_token交换 access_token 的代码时出现错误 400 和 415
【发布时间】:2017-02-04 16:29:39
【问题描述】:

我正在编写一个应用程序来授权自己进入 Spotify。我使用 node app.js 示例来让事情正常工作,现在本机重写到 Objective C。我已经通过回调函数提取了授权代码

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

然后我发布此请求并获取 {"error":"server_error","error_description":"意外状态:415"} 返回。

我发现关于这个主题的各种问题都围绕着 Content-Type 需要是 application/x-www-form-urlencoded ,但这不是直接的解决方法。

有人对我有什么建议吗?

【问题讨论】:

  • 我应该说使用 Postman 我已经证明 base64 编码是正确的,并且使用 'application/x-www-form-urlencoded' 可以正常工作并为我提供 access_code

标签: objective-c spotify


【解决方案1】:

我在这里找到了答案NSURLRequest : Post data and read the posted page

HTTPBody 中的原始数据似乎需要采用不同的格式——根本不需要 JSON。上面的链接有一个有用的功能,我稍微更新了

-(NSData*)encodeDictionary:(NSDictionary*)dictionary {

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

}

和..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2016-08-03
    • 2020-06-05
    • 2020-12-07
    • 2017-06-18
    相关资源
    最近更新 更多