Showing results for 
Search instead for 
Did you mean: 

Create OAuth2 token via groovy

Occasional Contributor

Create OAuth2 token via groovy



I want to authorize a rest service by passing the token via the header.


I know that you can enter all necessary details in "authentication"-tab.  (see screenshot)



Is there a way to create a token (with same parameters as mentioned in the screenshot) via groovy script?

(so that I don't have to create a new profile via GUI)


Once token is generated by groovy script, I can store it as property value and link it to the rest service via header parameter.


e.g. groovy script provide "xxxxxxx" as token, which I store in "Token"-parameter (part of "Properties")

In header of rest service request, I can add following parameter: "Authorization" and with value "${Properties#Token}



Can someone help me please with the creation of the groovy script?  (to receive the OAuth2 token)


Thanks in advance!

Community Hero

Re: Create OAuth2 token via groovy

Hey @tsjakamaka,

What youre asking is possible but it's a lot of work.

I had to setup an OAuth v2.0 process just using individual REST requests (rather than using the OAuth v2.0 setup functionality) and it was difficult.

If you want to set it up in either groovy or REST requests (which is easier) you'll need to identify all the individual requests that are exercised during the OAuth v2.0 process. Without this info people arent going to be able to help.

There are going to be at least 5 individual requests (i actually had 12) that youll need to identify before you can start trying to set this up.


if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Occasional Contributor

Re: Create OAuth2 token via groovy

Hi @richie ,


Why is it so difficult?


I would expect that it should work if I use following code:


oAuthProfile.setClientID("client id")
oAuthProfile.setClientSecret("client secret")
oAuthProfile.setAuthorizationURI("authorization URL" )
oAuthProfile.setAccessTokenURI("AccessToken URL" )
oAuthProfile.setRedirectURI("Redirect URL" )

def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS);
oAuthClientFacade.requestAccessToken(oAuthProfile, true);


But I don't know how I can create the "oAuthProfile"-object.  (and off course I need to update the parameters.  (same as entered in authorization screen)

New Here?
Join us and watch the welcome video:
Top Kudoed Authors