2017-08-10 4 views
0

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

誰かが実際に間違っていることを理解するのを手伝ってもらえますか? ありがとうございます。

+0

NSURLConnectionが、それは私の問題を解決するiOSの10 –

+0

に推奨されませんので、あなたはURLSessionを使用する必要がありますか?それとも単なる提案ですか? – user2908751

+0

それは示唆であり、それを試すことができます –

答えて

0

私は、あなたのコードとログを走った私は取得していますが

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified 
hostname could not be found." UserInfo= 
{NSUnderlyingError=0x608000059aa0 {Error Domain=kCFErrorDomainCFNetwork 
Code=-1003 "A server with the specified hostname could not be found." 
UserInfo={NSErrorFailingURLStringKey=http://oauth- 
test.websitename.com/token, NSErrorFailingURLKey=http://oauth- 
test.websitename.com/token, _kCFStreamErrorCodeKey=8, 
_kCFStreamErrorDomainKey=12, NSLocalizedDescription=A server with the 
specified hostname could not be found.}}, 
NSErrorFailingURLStringKey=http://oauth-test.websitename.com/token, 
NSErrorFailingURLKey=http://oauth-test.websitename.com/token, 
_kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, 
NSLocalizedDescription=A server with the specified hostname could not 
be found.} 

私もNSUrlSessionでそれを試してみましたが、それは同じ問題を与えているです。 私はあなたのサーバが以下

に設定されていないと考えているURLのセッションでコード

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:@"http://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]; 

NSURLSession * session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
NSURLSessionDownloadTask *dataTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    NSLog(@"%@", response); 

}]; 

[dataTask resume]; 
+0

はい、私のコードはサーバーに届かないと確信しています。私はURLとobject []の実際の値を変更しました。しかし、はい、私は今あなたとPhu Duyによって提案されるごとにNSURLSessionで試しています – user2908751

+0

私はあなたに何を言っているのですか、上記はNSUrlSessionのコードです。私はあなたのリンクに問題があると思う。もしあなたが私にそれを試すことができるようないくつかの他のものを提供することができます。 –

関連する問題