尝试访问 rapidAPI yahoo finance 通过 swift 给我错误 403

Attempts to access rapidAPI yahoo finance give me error 403 through swift

我正在尝试使用 rapidAPI 访问 Yahoo Finance API 进行个人项目。我注册了一个免费帐户并获得了 API 密钥。 Rapid API 允许他们的用户复制和粘贴代码来请求数据,所以我拿了他们的演示代码来访问 API。这是 swift 在尝试访问有关股票的详细信息时使用的:

import Foundation

let headers = [
"x-rapidapi-host": "apidojo-yahoo-finance-v1.p.rapidapi.com",
"x-rapidapi-key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" //private key 
]

let request = NSMutableURLRequest(url: NSURL(string: "https://apidojo-yahoo-finance- 
v1.p.rapidapi.com/stock/get-detail?region=US&lang=en&symbol=APPL")! as URL,
                                    cachePolicy: .useProtocolCachePolicy,
                                timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
    }
})

dataTask.resume()

当我运行这个代码时,它给我错误403。这意味着根据他们的website,这个数据是被禁止的。我试过使用其他 APIs 并复制和粘贴演示代码,它们工作正常。这个 API 不应该贬值,因为它在网站演示中 运行ning。这是我在 Xcode 中收到的返回消息:

我刚刚查看了 Yahoo Finance API。 GET /get-detail 端点现在似乎已被弃用。尽管它已被弃用,但它仍然对我有用。

确保订阅此 API。这可能是 403 代码的原因