Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error Unsupported Grant type?

I am not able to login in my Application which uses Oath-2 and getting error of unsupported_grant_type.I am using Alamofire to POST login data but not able to succeed.Where i am doing Wrong ?I am not able to solve the issue.

func sendFeedback()

func sendFeedback(){


        let parameters = [

            "UserName": username_textfield.text! as String,
            "Password": password_textfield.text! as String,
            "grant_type": "Password" as String,
                    ]


        Alamofire.upload(multipartFormData: { multipartFormData in

            for (key, value) in parameters {
                multipartFormData.append((value.data(using: .utf8))!, withName: key)
            }}, to: "http://192.168.100.5:84/Token", method: .post, headers: ["Authorization": "auth_token"],
                encodingCompletion: { encodingResult in
                    switch encodingResult {
                    case .success(let upload, _, _):
                        upload.response { [weak self] response in
                            guard self != nil else {
                                return
                            }
                            debugPrint(response)
//                            self?.view.hideToastActivity()
                            self?.view.makeToast(message: "Send Successfull. !!!")

                        }
                        upload.uploadProgress(queue: DispatchQueue(label: "uploadQueue"), closure: { (progress) in


                        })
                    case .failure(let encodingError):
                        print("errorss:\(encodingError)")
                    }
        })

    }

I am getting status code of 400.How this issue be Solved?

like image 541
Bikesh Thakur Avatar asked Jun 22 '17 08:06

Bikesh Thakur


People also ask

What is Grant type in Web API?

Application grant types (or flows) are methods through which applications can gain Access Tokens and by which you grant limited access to your resources to another entity without exposing credentials. The OAuth 2.0 protocol supports several types of grants, which allow different types of access.

What is Grant type in Salesforce?

To integrate an external web app with the Salesforce API, use the OAuth 2.0 web server flow, which implements the OAuth 2.0 authorization code grant type. With this flow, the server hosting the web app must be able to protect the connected app's identity, defined by the client ID and client secret.

What is Grant_type Client_credentials?

With the client credentials grant type, an app sends its own credentials (the Client ID and Client Secret) to an endpoint on Apigee Edge that is set up to generate an access token. If the credentials are valid, Edge returns an access token to the client app.


1 Answers

Try this,this may help you

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

           "UserName": username_textfield.text! as String,
           "Password": password_textfield.text! as String,
           "grant_type": "password",
                   ]
       //        let url = NSURL(string: "http://192.168.100.5:84/Token")!
       Alamofire.request("http://192.168.100.5:84/Token", method: .post, parameters: parameters, encoding:  URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in

           switch(response.result) {

           case .success(_):
               if response.result.value != nil{
//                    print(response.result.value ?? )
                   let statusCode = (response.response?.statusCode)!
                   print("...HTTP code: \(statusCode)")
               }
               break

           case .failure(_):
//                print(response.result.error ?? )
               break

           }
       }
like image 158
seon Avatar answered Oct 03 '22 07:10

seon