I’m working on a personal iOS app and need to get stock market data from Yahoo Finance through RapidAPI. I signed up for their free tier and received my API credentials.
Here’s the Swift code I’m using to fetch company information:
import Foundation
let apiHeaders = [
"x-rapidapi-host": "yahoo-finance3.p.rapidapi.com",
"x-rapidapi-key": "your-api-key-here"
]
let apiURL = "https://yahoo-finance3.p.rapidapi.com/v1/finance/result?region=US&lang=en&ticker=TSLA"
let urlRequest = NSMutableURLRequest(url: URL(string: apiURL)!,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 15.0)
urlRequest.httpMethod = "GET"
urlRequest.allHTTPHeaderFields = apiHeaders
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: urlRequest as URLRequest) { (responseData, urlResponse, requestError) in
if let error = requestError {
print("Request failed: \(error)")
} else {
if let httpResp = urlResponse as? HTTPURLResponse {
print("Status code: \(httpResp.statusCode)")
}
}
}
task.resume()
Every time I run this I get a 403 forbidden response. Other RapidAPI endpoints work perfectly with similar code patterns. The Yahoo Finance API appears to be active since it works in their online testing tool. What could be causing this authorization issue?