gpt4 book ai didi

json - 错误 : Error Domain=HTTPTask Code=404 "page not found" UserInfo=0x608000228720 {NSLocalizedDescription=page not found}

转载 作者:行者123 更新时间:2023-11-28 05:32:08 27 4
gpt4 key购买 nike

因为我是 Swift 编程语言的新手。我正在构建 mac 应用程序。我必须通过将 httprequest 发送到我的 api 来与我的 api 集成。

我正在为 http 请求使用子模块 SwiftHTTP。

这是我的代码:

import Cocoa

import SwiftHTTPOSX

class ViewController: NSViewController {

@IBOutlet weak var EmailField: NSTextField!

@IBOutlet weak var PasswordField: NSSecureTextField!

@IBAction func signin(sender: AnyObject) {
println("email is \(EmailField.objectValue) and Password is \(PasswordField.objectValue)")

var request = HTTPTask()
request.GET("https:user@gmail.com:password@api.domain.co/api/v3/auth/token/", parameters: nil, success: {(response: HTTPResponse) in
if let data = response.responseObject as? NSData {
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
println("response: \(response.responseObject)")
}
}, failure: {(error: NSError, response: HTTPResponse?) in
println("error: \(error)")
})

}
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}

}

错误:

error: Error Domain=HTTPTask Code=401 "accessed denied" UserInfo=0x60000002ae20 {NSLocalizedDescription=accessed denied}

预期输出描述:

当我向“http:user@gmail.com:password@api.domain.co/api/v3/auth/token”发送请求时,我将在响应中获得 apikey。但是上面的错误杀死了我。

最佳答案

根本不需要使用 SwiftHTTP。而且您使用的格式 user@email.com:password@api.domain/com/.... 在您使用 Javascript 或 Jquery 时也可以使用。

在 Swift 中,它不接受 basic-auth。对于基本身份验证,我们必须将编码字符串发送到身份验证 url,如:

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "\(Email.stringValue):\(Password.stringValue)"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(nil)
let authString = "Basic \(base64EncodedCredential)"
println("\(authString)")
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)



let url = NSURL(string: "https://api.domain.com/api/v2/auth/token/")
let task = session.dataTaskWithURL(url!) {
(let data, let response, let error) in
if let httpResponse = response as? NSHTTPURLResponse {
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
}
}

然后把数据打印出来看看肯定能满足你的问题。

关于json - 错误 : Error Domain=HTTPTask Code=404 "page not found" UserInfo=0x608000228720 {NSLocalizedDescription=page not found},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27521212/

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