Grails RestBuilder simple POST example

后端 未结 3 1711
谎友^
谎友^ 2020-12-29 07:17

I\'m trying to do an OAuth2 user-credentials post to an OAuth2 service using the Grails RestBuilder plugin.

If I try to specify the post body as a map, I get an erro

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 08:03

    Following code works for Box connection. Spend few of hours figuring this out

        String pclient_id = grailsApplication.config.ellucian.box.CLIENT_ID.toString()
        String pclient_secret=grailsApplication.config.ellucian.box.CLIENT_SECRET.toString()
        String pcode = params.code
    
        log.debug("Retrieving the Box Token using following keys Client ID: ==>"+pclient_id+"<== Secret: ==>"+pclient_secret+"<== Code: ==>"+pcode)
        RestBuilder rest = new RestBuilder()
    
        MultiValueMap form = new LinkedMultiValueMap()
        form.add("client_id", pclient_id)
        form.add("client_secret", pclient_secret)
        form.add("grant_type", "authorization_code")
        form.add("code", pcode)
        def resp = rest.post("https://app.box.com/api/oauth2/token") {
            accept("application/json")
            contentType("application/x-www-form-urlencoded")
            body(form)
        }
    
        def js = resp.json.toString()
        println("sss"+js)
        def slurper = new JsonSlurper()
        def result = slurper.parseText(js)
        println("Message:"+result.error)
    
        render js
    

提交回复
热议问题