我有一小段Java代码,我必须将其合并到Jmeter中以调用包含OAuth身份验证的特定Jar文件。
Java Code:-
package Name1;
import Service;
public class OAuth {
public static void main(String[] args) {
String requestPayload ="";
OAuth service = new OAuth("${Key1}", "${Key2}")
.setRequestTokenURL("${URL1}")
.setAccessTokenURL("${URL2}")
.setAccessResourceURL("${URL3}")
.setRequestorId("${Email}")
.setScope("${Status}");
String[] authenticatedAccessToken = service.getAuthenticatedAccessToken();
String resourceResponse = service.accessResource(requestPayload, authenticatedAccessToken);
System.out.println(resourceResponse);
}
Bean Shell Sampler代码如下
我们尝试了这段代码,但它无效。
import Service;
key = vars.getObject("${AppKey}");
secret = vars.getObject("${SecretKey}");
Email = vars.getObject("${Email}");
Scope = vars.getObject("${Scope}");
requestPayload =vars.getObject("");
OAuth service = new OAuth(key, secret);
service.setRequestTokenURL("${URL1}");
service.setAccessTokenURL("${URL2}");
service.setAccessResourceURL("${URL3}}");
service.setRequestorId(${Email});
service.setScope(${Scope});
authenticatedAccessToken = service.getAuthenticatedAccessToken();
resourceResponse = service.accessResource(requestPayload, authenticatedAccessToken);
SampleResult.setResponseData(resourceResponse);
有人可以帮助我们纠正我们,因为这段代码出了问题。感谢
答案 0 :(得分:0)
首先确保提供Service命名空间的.jar文件存在于JMeter类路径(通常是JMeter安装的/ lib文件夹)中,然后重新启动JMeter以获取它们。
尝试以下方法:
import Service;
key = vars.getObject("AppKey");
secret = vars.getObject("SecretKey");
Email = vars.getObject("Email");
Scope = vars.getObject("Scope");
requestPayload =vars.getObject("");
OAuth service = new OAuth(key, secret);
service.setRequestTokenURL(vars.get("URL1"));
service.setAccessTokenURL(vars.get("URL2"));
service.setAccessResourceURL(vars.get("URL3");
service.setRequestorId(Email);
service.setScope(Scope);
authenticatedAccessToken = service.getAuthenticatedAccessToken();
resourceResponse = service.accessResource(requestPayload, authenticatedAccessToken);
SampleResult.setResponseData(resourceResponse);
vars.get()
或vars.getObject()
时,您不需要使用${}
Email
)时,您也不需要这些数字括号有关调试Beanshell脚本的一些提示:
debug();
指令 - 您将在stdout中看到详细的调试信息如果您将代码包装到"尝试"阻止并设置" catch"阻止像:
catch (Throwable ex) {
log.error("Somewhing wrong: " , ex);
}
您将在 jmeter.log 文件中看到堆栈跟踪
您还可以考虑将JSR223 Sampler和groovy语言切换为
有关更详细的说明,有关安装groovy脚本引擎和脚本最佳实践的说明,请参阅Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!文章。