我将如何以字符串形式将 JSON 响应与我的代码一起返回?
目的:我想在getAccessToken()
从 json 响应体获取 accessToken 后调用它,需要将其作为String
返回以用于其他方法。
我试图获得的响应示例:
"accessToken" : "The ID I need from here"
法典:
private String apiAccessToken;
public JsonPath getAccessToken() {
JsonPath jsonPath = given().header("X-API-KEY", Config.API_KEY).header("session", this.sessionID)
.header("username", this.userNameId).queryParam("code", verifiedCode).log().all()
.get(baseUri + basePath + "/vm1/verifyCode").then().log().all().extract().jsonPath();
this.apiAccessToken = jsonPath.get("accessToken");
return new JsonPath(apiAccessToken);
}
[已添加 - 从下面的评论中显示我如何使用解决方案]
如何调用此方法的示例
public static String getToken(String key) {
String res = given()
.header("X-API-KEY", Config.API_KEY)
.header("session", this.SessionId)
.header("username", this.UserName)
.queryParam("code", verifiedCode)
.log().all()
.get(baseUri + basePath + "/vm1 /verifyCode")
.then()
.log().all()
.extract().asString();
JsonPath js = new JsonPath(res);
return js.get(key).toString();
}
public static String getJsonPath(Response response, String key) {
String complete = response.asString();
JsonPath js = new JsonPath(complete);
return js.get(key).toString();
}
@Test
public void testAuthValidator() throws InterruptedException, IOException, GeneralSecurityException {
String sentCode = GmailUtility.getVerificationCode(); // Uses GMAIL API service to to read and get code from email and sends to getAccessToken
System.out.println(sentCode);
String Token = getToken("accessToken"); // Validates verification code. Spits out response for accessToken
System.out.println(validator);
driver = initializeDriver(); // Invokes Chrome
driver.get(env.API_Website); // Goes to api website
AuthApi auth = new AuthApi(driver);
auth.getAuthorizeButton().click(); // Clicks a text field on webpage
auth.getValueField().sendKeys("Token " + Token); // Need to call extracted response for the accessToken from getAccessToken.
只需编写一个简单的可重用方法来使用 JSONPath 提取值,并在代码中调用该方法,下面是一个示例
可重复使用的方法:
public static String getJsonPath(Response response, String key) {
String complete = response.asString();
JsonPath js = new JsonPath(complete);
return js.get(key).toString();
}
测试:
public static void main(String[] args) {
Response res = given().header("X-API-KEY", Config.API_KEY).header("session", this.sessionID)
.header("username", this.userNameId).queryParam("code", verifiedCode).log().all()
.get(baseUri + basePath + "/vm1/verifyCode").then().log().all().extract().response();
String value = getJsonPath(res, "accessToken");
System.out.println(value);
}
更新:
public static String getToken(String key) {
String res = given().header("X-API-KEY", Config.API_KEY).header("session", this.SessionId)
.header("username", this.UserName).queryParam("code", verifiedCode).log().all()
.get(baseUri + basePath + "/vm1 /verifyCode").then().log().all().extract().asString();
JsonPath js = new JsonPath(res);
return js.get(key).toString();
}
@Test
public void testAuthValidator() throws InterruptedException, IOException, GeneralSecurityException {
String sentCode = GmailUtility.getVerificationCode();
System.out.println(sentCode);
String Token = getToken("accessToken");
System.out.println(Token);
driver = initializeDriver();
driver.get(env.API_Website);
AuthApi auth = new AuthApi(driver);
auth.getAuthorizeButton().click();
auth.getValueField().sendKeys("Token " + Token);
}
您可以使用此值