Java 如何使用REST-Assured自动获取承载令牌

Java 如何使用REST-Assured自动获取承载令牌,java,oauth,postman,rest-assured,bearer-token,Java,Oauth,Postman,Rest Assured,Bearer Token,我目前正在使用Postman生成承载令牌,我正在自动测试中使用该令牌。现在,我还想使用Java中的REST Assured自动化承载令牌生成过程。请帮帮我。谢谢 这是有效的。谢谢@wilfred clement public static String getOauthToken(String consumerKey, String consumerSecret, String endpoint ) { log.info("GET ACCESS TOKEN=" + endpoint); UR

我目前正在使用Postman生成承载令牌,我正在自动测试中使用该令牌。现在,我还想使用Java中的REST Assured自动化承载令牌生成过程。请帮帮我。谢谢


这是有效的。谢谢@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);
}

}

请发布您的邮递员设置的屏幕截图,您必须生成承载令牌,我们可以提供帮助
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);
}