iOS 缺少 grant_type

标签 ios http post oauth

我在第一次尝试请求访问 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/

相关文章:

php - 如何在 PHP 中解析 JSON 对象?

javascript - 是否可以使用 REST API 将项目添加到 Sharepoint 2010 中的站点级别列表?

ios - ios中uiscrollview的底部刷新控件

objective-c - 键盘遮挡了输入字段

http - 强制将域名中的字母大写

c++ - Delphi/C++Builder 10.2.3 RESTRequest POST

iphone - 在 iPhone 底部添加 StatusBar

ios - NSFetchedResultsController 委托(delegate)方法调用谓词更改

c# - 如何在IIS上产生负载?

android - 如何连接到 Android 上的代理?