gpt4 book ai didi

ios - 下载带有 "Authorization" header 的文件时出现 400 错误

转载 作者:行者123 更新时间:2023-12-01 21:59:15 26 4
gpt4 key购买 nike

服务器返回一个 json 文件,即:

{"ctrl":{"code":400,"text":"Not valid Access token","ts":"2020-03-05T11:54:01.547Z"}}

代码:
public func startDownload(url: URL, pathURL: URL) {
let accessToken: String! = "Bearer \(Constants.access_token)"
self.dirURL = pathURL
var request = URLRequest(url: url)
guard let token = accessToken else { return }
request.addValue(token, forHTTPHeaderField: "Authorization")
downloadTask = backgroundSession.downloadTask(with: request)
downloadTask.resume()
}

仅供引用:访问 token 有效,它正在与 Postman 一起使用。

最佳答案

你会遇到问题,因为不幸的是,这个问题没有好的解决方案。 AuthorizationReserved HTTP Headers 之一并将其设置为 URLRequest header ,或在 URLSessionConfiguration.httpAdditionalHeaders可能根本行不通:

If you set a value for one of these reserved headers, the system may ignore the value you set, or overwrite it with its own value, or simply not send it.



有人可能期望您可以在 URLSessionTaskDelegate 中提供此 token 。方法 urlSession(_:task:didReceive:completionHandler:)它处理身份验证挑战,但您需要在其中提供 URLCredential对象,遗憾的是它没有采用 Bearer 的构造函数 token ,所以这是不行的。

所以基本上,不用自己写 URLProtocol实现时,最好的办法是在一些额外的自定义 header 字段中发送 token ,并让服务器从那里获取它(如果您可以控制服务器代码)。 Source

关于ios - 下载带有 "Authorization" header 的文件时出现 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60546155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com