gpt4 book ai didi

ios - swift 中 Braintree 集成期间需要授权错误

转载 作者:行者123 更新时间:2023-11-29 05:32:47 25 4
gpt4 key购买 nike

我正在尝试将 Braintree 支付方法集成到我的 swift 代码中,但我被困在这里,它因错误而中断

{"error":{"statusCode":401,"name":"Error","message":"Authorization Required","code":"AUTHORIZATION_REQUIRED"}}

我所做的与 Braintree 文档中提到的完全相同。我不知道它需要哪种授权,我确实在用户登录时分配了一个授权 token ,我想知道它是否需要该授权 token ,但在此代码中没有这样的参数,我应该在其中放置该 token 用于生成用于付款方式的客户端 token 。这里执行时的打印语句在日志中给出了这个,“客户端 token 是:

{"error":{"statusCode":401,"name":"Error","message":"Authorization Required","code":"AUTHORIZATION_REQUIRED"}}", I am bit confused in its calling also. I have just started these thing so I am very sorry I have done any obvious mistake. Thanks.

        // TODO: Switch this URL to your own authenticated API

let clientTokenURL = NSURL(string: "https://braintree-sample-
merchant.herokuapp.com/client_token")!
let clientTokenRequest = NSMutableURLRequest(url:
clientTokenURL as URL)
clientTokenRequest.setValue("text/plain", forHTTPHeaderField:
"Accept")
URLSession.shared.dataTask(with: clientTokenRequest as
URLRequest) { (data, response, error) -> Void in
// TODO: Handle errors
if let error = error {
print("Error: \(error.localizedDescription)")

} else {
print("in Session")
let clientToken = String(data: data!, encoding:
String.Encoding.utf8)!
print("Client Token is : \(clientToken)")

}

}.resume()
}

最佳答案

必须在 header 中提供授权 token 才能避免此错误。相反,这个版本的代码可以正常工作。

       completionHandler:@escaping (_ response: NSDictionary?, _ error: Error?) - 
> ()) {



var headers: HTTPHeaders

// pass the authToken when you get when user login
let authToken = getAuthorizationToken()

if(self.isValidString(object: authToken as AnyObject)) {
headers = ["Authorization": authToken,
"Content-Type": "application/json",
"Accept": "application/json"]

} else {
headers = ["Content-Type": "application/json"]
}

AF.request(apiURL, method: .get, parameters: params as? Parameters,
encoding: JSONEncoding.default, headers: headers).validate().responseJSON
{
response in
self.handleResposne(response: response) { (response, error) in
completionHandler(response, error)
}
}
}

关于ios - swift 中 Braintree 集成期间需要授权错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57378536/

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