gpt4 book ai didi

ios - 发布请求时出现 NSURLSession 序列化错误

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:18 25 4
gpt4 key购买 nike

我正在尝试获取 Instagram 的访问 token ,但经常出现错误You must provide client id我认为这是一个序列化问题,因为我的参数没有正确序列化。我在 POSTMAN 上试过了,它运行良好,但在 NSURLSession 上它会抛出错误。

错误

{
code = 400;
"error_message" = "You must provide a client_id";
"error_type" = OAuthException;
}

我在序列化时做错了什么。

let instaInfoDict = NSDictionary(objects: ["client_id","client_secret","grant_type","redirect_uri","code"], forKeys: [kCLientIdInstagram,kCLientSecretIdInstagram,"authorization_code",kRedirectUriInstagram,code])
// Hit post request with params
WebServices().postRequest(kAccessTokenGenerationInstagram, bodyData: instaInfoDict, completionBlock: { (responseData) in
print(responseData)

})


//WEBSERVICE CLASS:

func postRequest(url:String, bodyData:NSDictionary, completionBlock:WSCompletionBlock){
if let url = NSURL(string: url){
let headers = [
"Accept": "application/json",
"content-type": "application/json"]

let session = NSURLSession.sharedSession()

let request = NSMutableURLRequest(URL: url, cachePolicy: .UseProtocolCachePolicy, timeoutInterval: 10)
request.HTTPMethod = "POST"
request.allHTTPHeaderFields = headers

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(bodyData, options: [.PrettyPrinted])

let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
if error != nil {
// Handle error


completionBlock!(responseData:nil)
}
else{
//Parsing the data
let parsedData = parseJson(response as? NSData)

completionBlock!(responseData: parsedData)

}

})

task.resume()
}


}

我也试过了

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(bodyData, options: NSJSONWritingOptions(rawValue: 0))

最佳答案

似乎问题在于 Content-Type 应该是 application/x-www-form-urlencoded 而不是 application/json,也是发送方式这种 Content-Type 的参数有一点变化。

我做了一个小测试,这段代码对我有用:

static func generateAccessToken() {

let params = ["client_id": "your_id",
"client_secret": "your_client_secret",
"grant_type": "authorization_code",
"redirect_uri": "http://tolkianaa.blogspot.com/",
"code": "the_code"]


guard let url = NSURL(string: "https://api.instagram.com/oauth/access_token") else {
return
}
let request = NSMutableURLRequest(URL: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"

let stringParams = params.paramsString()
let dataParams = stringParams.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let paramsLength = String(format: "%d", dataParams!.length)
request.setValue(paramsLength, forHTTPHeaderField: "Content-Length")
request.HTTPBody = dataParams

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
var json: AnyObject = [:]

guard let data = data else {
return
}

do {
json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
} catch {
// Do nothing
}

print(json)
}

task.resume()
}

为了获取字符串形式的参数,我做了这个扩展:

extension Dictionary {

func paramsString() -> String {
var paramsString = [String]()
for (key, value) in self {
guard let stringValue = value as? String, let stringKey = key as? String else {
return ""
}
paramsString += [stringKey + "=" + "\(stringValue)"]

}
return (paramsString.isEmpty ? "" : paramsString.joinWithSeparator("&"))
}

希望对您有所帮助!

关于ios - 发布请求时出现 NSURLSession 序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37650875/

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