简体   繁体   English

我需要帮助在我的 API 代码中实现不记名令牌,但我不知道如何:(

[英]I need help implementing Bearer Token in my API code but I don't know how :(

I wrote the code for my API but I didn't wrote the code for the Bearer Token since I don't know how to do it properly since I am new to swift:) so if anyone could help me or reference me to a link on how to do it:)我为我的 API 编写了代码,但我没有为承载令牌编写代码,因为我不知道如何正确执行,因为我是 swift 的新手 :) 所以如果有人可以帮助我或引用我的链接关于如何做到这一点:)

This Is all I have about it in my code but its not working:(这就是我在代码中的全部内容,但它不起作用:(

                let headers = [
                "content-type": "application/json",
                "authorizetoken": "NjQzODM2N0NDNDM4NDhCNDk3RkU0NjA0QUY0NjVENkE=",
                "cache-control": "no-cache",
                ]
    
    }
    
    var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 120)
            request.httpMethod = "GET"
            request.allHTTPHeaderFields = headers
import Foundation
func ApiCall(urlString:String,completion:@escaping (_ data:Data?,_ error:Error?)->Void) {
   let url = URL(string: urlString)!

   var request = URLRequest(url: url)
   request.allHTTPHeaderFields = [
            "content-type": "application/json",
            "authorizetoken": "NjQzODM2N0NDNDM4NDhCNDk3RkU0NjA0QUY0NjVENkE=",
            "cache-control": "no-cache",
            ]
//// you can write "Bearer" if you are requesting url as a user from server like its only use for you 
////if not only for you but for a group of users or for an app you shold only wirte the token string


URLSession.shared.dataTask(with: request) { (data, response, error) in
  guard error == nil else { return completion(nil,error) }

  guard let data = data, let response = response else { return }
  let yourdata = data
  completion(data,nil)
  print(data,response)
  // handle data
}.resume()


}

and you can use it like你可以像这样使用它

let data = ApiCall(urlString:"http:example.com") { data, error in
  if let _data = data {
 // handle your data here 
    }
  if let err = error {
 // handle your error here 
    }
}

You need to update your allHTTPHeaderFields您需要更新您的 allHTTPHeaderFields

request.allHTTPHeaderFields = [
            "content-type": "application/json",
            "authorizetoken": "Bearer NjQzODM2N0NDNDM4NDhCNDk3RkU0NjA0QUY0NjVENkE=",
            "cache-control": "no-cache",
            ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道如何将弹出框与按钮对齐 - I don't know how to align my popovers to button 我不知道我的代码有什么问题(带有MWFeedParser的RSS阅读器) - I don't know what's wrong with my code (rss reader with MWFeedParser) 我有一个有效的滑动检测器脚本,但我不知道如何将它连接到我的角色控制器 - I have a working swipe detector script, but I don't know how to connect it to my character controller 我不知道为什么这个表视图代码崩溃了 - i don't know why this table view code crashes 我不知道为什么结果不是我想的 - I don't know why the result is not my have thought 我的应用由于高度限制而崩溃,我不知道如何解决 - My App crashes because of the heightConstraints I don't know how to fix it 如果我不知道我需要使用的所有不安全域,那么应用程序传输安全 - App Transport Security if I Don’t Know All the Insecure Domains I Need to Use 为什么我不需要在此代码中使用CFRetain()ABAddressBookGetPersonWithRecordID()的结果 - Why don't I need to CFRetain() the result of ABAddressBookGetPersonWithRecordID() in this code 如果不知道其密钥,如何在Firebase中更新节点? - How to update a node in Firebase if I don't know its key? 我不知道如何在Swift中将屏幕截图发送到Instagram应用程序 - I don't know how to send screenshot to Instagram application in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM