OAuth2 token request using JSON fails - grant type not supported

  • I'm building a small application in Go that will access Salesforce using REST. I chose to use the full OAuth webflow.

    Documentation for the Web Server Authentication Flow

    I'm able to do the full flow when I use urlencoded form data in the token request (number 3 in the flow chart in the documentation).

    func httpPostXCoded(c appengine.Context, theUrl string, attrs map[string]string, OAS OAuthSecurity) (body []byte, err error) {
        var myUrlEncoded = ""
        for key, val := range(attrs) {
            if len(myUrlEncoded) > 0 {
                myUrlEncoded += "&"
            }
            myUrlEncoded += key + "=" + url.QueryEscape(val)
        }
        req, err := httpRequest("POST", theUrl, bytes.NewReader([]byte(myUrlEncoded)))
        if err != nil {
            return
        }
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
        res, err := httpClient(c).Do(req)
    
        // cut code
    }
    

    I would however like to use a JSON-formatted request - I changed the code to look like this

    func httpPostXCoded(c appengine.Context, theUrl string, attrs map[string]string, OAS OAuthSecurity) (body []byte, err error) {
        var rbody []byte
        var req *http.Request
        if rbody, err = json.Marshal(attrs); err != nil {
            return
        }
        req, err = httpRequest("POST", theUrl, bytes.NewReader(rbody))
        if err != nil {
            return
        }
        req.Header.Add("Content-Type", "application/json")
        res, err := httpClient(c).Do(req)
    
        // cut code
    }
    

    Salesforce won't send me a token but an error message like this

    {"error_description":"grant type not supported","error":"unsupported_grant_type"}
    

    As you can see in the code snippet above I am setting the Content-Type to json. In both cases I have a key in the map called "format" with the value "json". The working urlencoded request will, correctly, give me json formatted data back.

    An example of the json data I'm sending is

    {"client_id":"","client_secret":"","code":"","format":"json","grant_type":"authorization_code","redirect_uri":"http://localhost:8080/callback"}
    

    Please note some field has intentionally been emptied.

    No, should be the same as I use in the first case. Only difference should be the encoding. I've added a debug print out of the json payload.

    I do not see any mention in the documentation you have linked to about support for a JSON format request, just mention of being able to choose the format of the response to be JSON. Do you have a reason to expect a JSON request to work?

    @Keith C you may be right. I was pretty sure I've read that it should support json - but going back to the documentation it never states what encodings that are supported for the call (inbound). The example only shows URL encoded encoding.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used