我在調用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);
}
}
];
有人可以試圖幫助我找出究竟是哪裏出了問題嗎? 在此先感謝。
您應該使用URLSession因爲NSURLConnection的是iOS的10 –
不贊成將它解決我的問題?或者這只是一個建議? – user2908751
這是一個建議,你可以試試 –