gpt4 book ai didi

java - 如何使用 REST Assured 自动获取 Bearer Token

转载 作者:行者123 更新时间:2023-12-02 10:46:23 25 4
gpt4 key购买 nike

我目前正在使用 Postman 来生成承载 token ,我在自动化测试中使用它。现在我也想使用 Java 中的 REST Assured 来自动化承载 token 生成过程。请帮我。谢谢。

enter image description here

Response response =
given()
.auth()
.oauth(
"n0pCrq5SPgraZ3CyY0Nd",
"xvvx-LVd5dszLi9OO_1qjbU4eUQ4dXwLrDZN7oioSITr_EXrgsyyOvPvZmv85Ew2",
"",
"",
"HMAC-SHA256")
.when()
.get(url)
.then()
.contentType(ContentType.JSON)
.extract()
.response();

最佳答案

这正在工作。谢谢@wilfred clement。

public static String getOauthToken(String consumerKey, String consumerSecret, String endpoint ) {

log.info("GET ACCESS TOKEN=" + endpoint);
URI uri = null;
try {
uri = new URI(endpoint);
} catch (URISyntaxException e) {
log.error("Not proper oauth url=" + endpoint);
throw new RuntimeException(e);
}

ValidatableResponse res = given()
.header("Content-Type", "application/json")
.auth().oauth(consumerKey,
consumerSecret,
"",
"")
.body("{\"grantType\": \"client_credentials\"}").when().post(uri).then();

int responseCode = res.extract().statusCode();

if (HttpURLConnection.HTTP_OK == responseCode) {
String token = res.extract().jsonPath().get("accessToken").toString();
log.info("Auth token=" + token);
return token;
} else {
String msg = "Access token retrieve failed. Http response code=" + responseCode;
log.error(msg);
throw new RuntimeException(msg);
}

}

关于java - 如何使用 REST Assured 自动获取 Bearer Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52537047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com