我正在尝试使用以下Groovy代码执行链接https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html上给出的自动OAuth检索:
// 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;
// Set up variables
def project = ModelSupport.getModelItemProject(context.getModelItem());
def authProfile = project.getAuthRepository().getEntry("OAuth 2");
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);
// Post the info to the log
log.info("Set new token: " + authProfile.getAccessToken());
但是我收到以下错误,它无法解析类TokenType:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 5: unable to resolve class com.eviware.soapui.support.editor.inspectors.auth.TokenType @ line 5, column 1. import com.eviware.soapui.support.editor.inspectors.auth.TokenType; ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class com.eviware.soapui.support.editor.inspectors.auth.TokenType @ line 5, column 1. at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:149) at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1225) at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:178) at org.codehaus.groovy.control.CompilationUnit$11.call(CompilationUnit.java:651) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:694) at groovy.lang.GroovyShell.parse(GroovyShell.java:706) at groovy.lang.GroovyShell.parse(GroovyShell.java:742) at groovy.lang.GroovyShell.parse(GroovyShell.java:733) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:136) at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:87) at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141) at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) 1 error
如果我不导入TokenType,那么我将得到错误No Such Property TokenType
。如何导入TokenType类,或者是否有其他方法可以在不使用TokenType的情况下请求新的访问令牌?
答案 0 :(得分:1)
我能够通过从代码中删除TokenType来自动执行OAuth令牌检索。可以在不指定TokenType的情况下实例化OltuOAuth2ClientFacade。以下是更新的代码:
// Import the required classes
import com.eviware.soapui.impl.rest.actions.oauth.*;
import com.eviware.soapui.model.support.ModelSupport;
//// Set up variables
def authContainer = testRunner.testCase.testSuite.project.OAuth2ProfileContainer
def authProfile = authContainer.getProfileByName("Profile 1")
def oldToken = authProfile.getAccessToken();
// Create a facade object
def oAuthFacade = new OltuOAuth2ClientFacade();
// Request an access token in headless mode
oAuthFacade.requestAccessToken(authProfile);
// Wait until the access token gets updated
while(oldToken == authProfile.getAccessToken()) {
}
// Post the info to the log
log.info("Set new token: " + authProfile.getAccessToken());