RESTful APIを呼び出す際に問題が発生しています。リクエストは、APIからトークンを取得することです。私はそれを使ってそのAPIをさらに呼び出す必要があります。Xcode httpsでRestful APIを呼び出す
問題は、私がその呼び出しを行うたびにHTTP 403ステータスコードが得られ、その理由がわからないことです。私はAndroidから同じ呼び出しを行っていますが、問題なく正常に動作します。
実際、私はxCodeで作業するための経験は全くありません。私はちょうど他の開発者によって書かれた既存のコードにいくつかの変更を実装しています。私は何かが間違っているのか、何をしているのか分かりません。
id keys[] = {@"grant_type", @"client_id", @"client_secret", @"username", @"password"};
id objects[] = {@"password", @"AppClientID", @"AppClientSecret", @"usernamehere", @"userpasswordhere"};
NSUInteger count = sizeof(objects)/sizeof(id);
NSDictionary *d = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:nil];
NSURL *url = [NSURL URLWithString:@"https://oauth-test.websitename.com/token"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Basic" forHTTPHeaderField:@"Authorization"];
[request setHTTPBody:jsonData];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
if(data.length > 0 && connectionError == nil) {
NSLog(@"Response --> %@ ", response);
}
}
];
誰かが実際に間違っていることを理解するのを手伝ってもらえますか? ありがとうございます。
NSURLConnectionが、それは私の問題を解決するiOSの10 –
に推奨されませんので、あなたはURLSessionを使用する必要がありますか?それとも単なる提案ですか? – user2908751
それは示唆であり、それを試すことができます –