{ error = “invalid_request”; “error_description” = “Required parameter is missing: grant_type”; } In swift

左心房为你撑大大i 提交于 2019-12-13 10:08:22

问题


I am trying with this but every time I will get 

{     error = "invalid_request";     "error_description" = "Required parameter is missing: grant_type"; }

Request String: 

let headers    = [ "Content-Type" : "application/x-www-form-urlencoded"]

let urlString = "https://accounts.google.com/o/oauth2/token?"

Alamofire.request(urlString, method: .post, parameters: ["grant_type":"authorization_code","code":"4/FAOYR1mQo1lN1Gdg9jDnigwZ8RP76NUrqPbYZlMCSP28","client_id":"387376833747-12pbtud9tepr4di0insdhc0d4qpf5e9m.apps.googleusercontent.com","client_secret":"xOacVhLavM9fH8SpOK4I2dRJ","redirect_uri":"https://stackoverflow.com"], encoding: JSONEncoding.default, headers : headers)
            .responseJSON { response in
                print(response)
                print(response.result)
        }

Also try with passing request parameter like this but still doesn't work for me.


回答1:


You can't send appended parameter with url while post request. Pass parameter as below this

  let headers    = [ "Content-Type" : "application/x-www-form-urlencoded","grant_type" : "authorization_code"" ]

        let urlString = "https://accounts.google.com/o/oauth2/token?"
            Alamofire.request(urlString, method: .post, parameters: ["code":"4/FAOYR1mQo1lN1Gdg9jDnigwZ8RP76NUrqPbYZlMCSP28","client_id":"apps.googleusercontent.com","client_secret":"","redirect_uri":"https://stackoverflow.com"], encoding: JSONEncoding.default, headers : headers)
                .responseJSON { response in
                    print(response)
                    print(response.result)
            }



回答2:


These error invalid_request occurs only below cases so please check with your server side what is missing or invalid.

The request is missing a required parameter, includes an unsupported or invalid parameter value, repeats a parameter,includes multiple credentials, utilizes more than one mechanism for authenticating the client, or is otherwise malformed.



来源:https://stackoverflow.com/questions/44801102/error-invalid-request-error-description-required-parameter-is-missin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!