How to really get the OAuth 2 token automatically
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to really get the OAuth 2 token automatically
I wrote some code that gets an access token. When the code runs, a browser displays on the screen which contains the access token.
But when I try to get the access token and log it, a null string is being displayed. Also, I do not know if there is a way to force the browser to close using my code. Right now when I run this code, the browser window opens but I have to click on it to close it.
Could you please let me know what I am doing wrong ?
Groovy Code:
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
def project = context.getTestCase().getTestSuite().getProject();
def oAuthProfile = project.getAuthRepository().getEntry("IMAGEN_Profile");
def clientSecret = testRunner.testCase.testSuite.getPropertyValue("Client_Secret")
def clientID = testRunner.testCase.testSuite.getPropertyValue("Client_ID")
oAuthProfile.setClientSecret(clientSecret);
oAuthProfile.setClientID(clientID);
log.info("Client Secret:"+clientSecret)
log.info("Client ID:"+clientID)
// the following code for getting new access token
def oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS);
oAuthClientFacade.requestAccessToken(oAuthProfile, true);
def accessToken = oAuthProfile.getAccessToken()
testRunner.testCase.testSuite.setPropertyValue("Auth_Code",accessToken)
log.info("Access Token:"+accessToken)
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I can't tell you what is wrong but I can post our script. :X Maybe it helps.
I didn't write it, so I can't answer any questions
We added this script as Event "SubmitListener.beforeSubmit
// Import the required classes import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade import com.eviware.soapui.support.editor.inspectors.auth.TokenType import com.eviware.soapui.model.support.ModelSupport def authProfileName = "PROFILE_NAME" if(!submit.getRequest().getAuthType().asBoolean()){ return // stop if the auth type is null, for example jdbc requests }else if(submit.getRequest().getActiveAuthProfile() == null){ return // stop if the auth profile is null }else if(authProfileName == submit.getRequest().getActiveAuthProfile().getName()){ // Set up variables def project = ModelSupport.getModelItemProject(context.getModelItem()) def authProfile = project.getAuthRepository().getEntry(authProfileName) def oldToken = authProfile.getAccessToken() def tokenType = TokenType.ACCESS // Create a facade object def oAuthFacade = new OltuOAuth2ClientFacade(tokenType) // Request an access token in headless mode oAuthFacade.requestAccessToken(authProfile, true, true) // Wait until the access token gets updated //while(oldToken == authProfile.getAccessToken()) {} //The sleep method can be used instead of a while loop //sleep(3000) for(int i = 0; i<=3000; i++){ if(oldToken != authProfile.getAccessToken()){ break } sleep(1) } // Post the info to the log //log.info("Set new token: " + authProfile.getAccessToken()) }
Cheers,
Cekay
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank Cekay, that didn't work for me, I must be missing something. First of all, all of the if code that is associated with the submit. didn't work. When I commented that out, the token is still blank. Do you have any Java code on the OAuth 2 script for page 1 and page 2.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Cekay,
I've never worked with Event Handlers, so it is probably my problem. Can anybody guide me?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'll try my best. There is not much to guide
- Click on "Events" in your SoapUI - It will open a new window
- Click on the green plus, to add a new event
- Choose "SubmitListener.beforeSubmit"
- Click OK and copy paste the script
- Change in line 6: PROFILE_NAME to the name of your auth-profile
- Save script via clicking OK
That is basically everything. Please find attached screenshot for more details ,
Run your testcase, where you need to get a token, and let the magic work for you 😉
I copied the script from my colleque, who is working on another project. All I did is to change the profile name so I think you don't have to change anything else. If this not working, you should show your error log.
Good Luck and have a nice weekend
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a question, where does the token go? Where do I retrieve it to put into my "Authorization" part of the header. I see in the loop that it waits for a new token and then just ends...
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah so that is the problem. Maybe we use the token in different ways. I don't have put it anywhere.
I have a testCase with a put rest request and use for the authorization my auth profile, which I used in the script.
It will generate the token for this profile. We don't have to send it via header.
When I start the testCase I will get a new token and it will be used directly.
See screenshot again please
So now we reached the end of my knowledge. If this is still not working for you, someone else has to help =/ Sorry
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here is the example and link to the event handlers article: https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html...
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks @Nastya_Khovrina,
I've read this article about 40 times and get nothing from it. I'm lost in the Auth Manager, I can't get an access token unless I use "Authorization Code Grant" and it only gives it to me in a web browser that I can do nothing with. Are there any classes on OAuth 2 with ReadyAPI? Maybe that would be helpful. Like a course for dummies. I'm not a dummy, but don't deal with websites very often, mostly do background stuff in PowerShell, Ruby, VBA and such.
