JSONSerialization.jsonObject

问题描述 投票:0回答:1

我是用JSON Token开发iOS的新手,我收到了一个不支持的_grant_type错误。我正在开发的代码如下

let params = ["grant_type": "password", "username": userEmail, "password": userPassword]

let url = URL(string: "http://test/SPUSR/Token")

var request = URLRequest(url: url!)

request.httpMethod = "POST"

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

try? request.httpBody = JSONSerialization.data(withJSONObject: params, options: [])

let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)

let task = session.dataTask(with: request) { (data, response, error) in
    if let data = data {
        let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]

        self.token = json!!["access_token"] as? String
    }

    if let error = error {
        print(error.localizedDescription)
    }
}

task.resume()

邮差一切都运作良好。我的API是在C#技术下开发的

ios swift nsjsonserialization
1个回答
0
投票

消息“unsupported_grant_type”明确告诉您服务器不期望该请求。您正在尝试POST:“grant_type”:“password”,正文中的凭据 - 这意味着内容类型必须是“application / x-www-form-urlencoded”。请参阅Postman中的控制台。

© www.soinside.com 2019 - 2024. All rights reserved.