2017-08-10 90 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

您應該使用URLSession因爲NSURLConnection的是iOS的10 –

+0

不贊成將它解決我的問題?或者這只是一個建議? – 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以及對象[]中的實際值。但是,是的,我現在正按照你和Phu Duy的建議嘗試使用NSURLSession – user2908751

+0

我告訴你的是,上面是NSUrlSession的代碼。我認爲你的鏈接存在問題。如果你可以給我一些其他像我可以嘗試一下。 –