我正在尝试使用以下API提取访问令牌:https://api.instagram.com/oauth/access_token。我已确保仅使用instagram应用程序ID,并且已将内容类型设置为“ x-www-form-urlencoded”,如以下答案所示:Authenticate the Test User { "error_type": "OAuthException", "code": 400, "error_message": "Invalid platform app" }。该API在邮递员中工作得很好,但是通过我的代码给出了以下错误响应:
{“ error_type”:“ OAuthException”,“ code”:400,“ error_message”:“无效的平台应用程序”}]
这是我的代码:
-(void)fetchAccessTokenForInsta{
NSString *targetUrl = [NSString stringWithFormat:@"%@", @"https://api.instagram.com/oauth/access_token"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSDictionary *tmp = @{@"client_id":@"126.....", @"client_secret":@"c79c.....", @"grant_type":@"authorization_code", @"redirect_uri":@"https://firebase.google.com/", @"code":_codeStr};
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:tmp options:0 error:&error];
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:targetUrl]];
NSDictionary *headers = @{@"Content-Type": @"application/x-www-form-urlencoded" };
[request setAllHTTPHeaderFields:headers];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
NSString *responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", responseStr);
}] resume];
}
其中_codeStr是从前面步骤中收到的代码中删除#后的字符串。任何帮助表示赞赏。
我在相同的情况下遇到相同的错误。