如何将AccessiToken添加到头并发送请求iOS Swift?

c90pui9n  于 2023-08-02  发布在  Swift
关注(0)|答案(1)|浏览(97)

我已经发送了一个带有访问令牌的请求。我得到了一个错误响应。

[Response]:
    [Status Code]: 401
  
   {"response":{"success":"false","result":{"errorCode":"4012001002","message":"Invalid token / expired."}}}

字符串
下面我添加了代码…

import UIKit
import Alamofire
import SwiftKeychainWrapper
class HelpViewController: UIViewController{

var ticketResult = [TicketResponse]()

let tokens: String? = KeychainWrapper.standard.string(forKey: "accessToken")
override func viewDidLoad()
{
    super.viewDidLoad()
    
    HistoryAPI()
}

func HistoryAPI() {
    if tokens != nil
    {
        print("Hello Access Token \(String(describing: tokens))")
            let headers: HTTPHeaders = [
                "content-type": "application/json",
                "correlationId": "myYt005",
                "operation": "ticketDetail",
                "destination": "CRM",
                "source": "WEB",
                "srDate": "16-05-2022",
                "token": "\(String(describing: tokens))"
                //   "token": "\(String(describing: token))"
            ]
        
            AF.request(ticketHistory_url, method: .get,  headers: headers).response { response in
                
                debugPrint(response)
                switch response.result {
                case .success(let data):
                 
                    if let ticketData = data as? [TicketResponse] {
                        self.ticketResult = ticketData
                    }
                case .failure(let error):
                    print("Error: \(error.localizedDescription)")
                    // Handle the error if needed
                }}}
    }
}

jv4diomz

jv4diomz1#

尝试使用正确的令牌头密钥“Authorization”

"Authorization": "Bearer \(accessToken)"

字符串

相关问题