我在第一次尝试请求访问 token 时收到 {"error_description":"Missing grant_type parameter value","error":"invalid_request"}。我的代码如下:
let params : [String : String] =
["client_id" : clientID,
"client_secret" : secret,
"redirect_uri" : redirectURL,
"code" : code,
"grantType" : "authorization_code"
]
var request = NSMutableURLRequest(URL: NSURL(string: url)!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("aapplication/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("\(request.HTTPBody!.length)", forHTTPHeaderField: "Content-Length")
可能是什么问题?提前谢谢你。
最佳答案
哇,我不想听起来很刺耳,但这段代码存在多个问题。
您没有发送 grant_type
,但您确实发送了 grantType
。
您将帖子正文编码为 application/json
,但您将 Content-Type 设置为 aapplication/x-www-form-urlencoded
,这似乎是 application/x-www-form-urlencoded
的拼写错误。
因此有两个可能的拼写问题和内容类型不匹配。
如果你需要发送 application/x-www-form-urlencoded 计算一下或 application/json .根据需要格式化数据并设置内容类型。
仔细检查所有内容的拼写。
关于iOS 缺少 grant_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26565495/