Forum Discussion

tsjakamaka's avatar
tsjakamaka
Occasional Contributor
5 years ago

Create OAuth2 token via groovy

Hi,

 

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!

2 Replies

  • richie's avatar
    richie
    Community Hero
    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.

    Cheers,

    Rich
    • tsjakamaka's avatar
      tsjakamaka
      Occasional Contributor

      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.setScope("scope")
      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)