Forum Discussion

sp00ky's avatar
sp00ky
Occasional Contributor
2 years ago

Automatic OAuth 2.0 access token refresh not working

Hi!   Does anyone know how to get automatic OAuth 2.0 access token refresh working before doing a REST call? Grant_type is "client_credentials" with client_id and client_secret. Manual refresh afte...
  • sp00ky's avatar
    2 years ago

    OK I fixed it by my own using a groovy script that executes before the REST request. This is not fixing the original problem that automatic access token refresh is not working, but ok. Learned a little more abour bearer token request and handling 🙂

     

    import wslite.rest.RESTClient
    import groovy.json.JsonSlurper
    
    def url = "https://xxx/api/oauth/token"
    def c_id = "yyy"
    def c_secret = "zzz"
    
    def client = new RESTClient(url)
    def response = client.post() {
    	type "application/x-www-form-urlencoded"
    	urlenc grant_type:"client_credentials", client_id:c_id, client_secret:c_secret
    }
    log.info response.statusCode + " - " + response.statusMessage
    
    def slurper = new JsonSlurper()
    def json = slurper.parseText(response.getContentAsString())
    log.info json.access_token
    
    return "Bearer " + json.access_token