簡體   English   中英

如何使用 Rest Assured 在位置響應標頭字符串中提取身份驗證代碼

[英]How to extract an auth code in Location response header String using Rest Assured

目前我有一個方法可以進行一些授權,它用於我在正文中的訪問令牌 POST 請求以建立正確的訪問令牌。 在我的授權方法中,我能夠在響應中提取 Location 標頭值。 唯一的問題是我只需要提取code=&之間的值,如下面的字符串( locationHeaderForAuthCode )所示:

https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64 -9855-074a57cc1570

由於該代碼用於我的訪問令牌 POST 請求中的請求正文,因此我只需要提取該部分。 我如何在 Rest Assured 中實現這一點,如果沒有,那么只需使用純 Java?

任何幫助,將不勝感激。 下面是我設置 Location 標頭的授權方法,以及訪問令牌發布請求的另一種方法:

private static void getAuthorizeCode() throws JsonException {

    response = given()
                .config(RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig()
                            .encodeContentTypeAs("x-www-form-urlencoded",
                                    ContentType.URLENC)))
            .cookies(cookies)
            .header("Accept-Encoding", "gzip, deflate, br")
            .formParam("redirect_uri", devRedirectUrl)
            .formParam("response_type", responseType)
            .formParam("client_id", userClientID)
            .formParam("client_secret", userClientSecrets)
            .formParam("decision", decision)
            .formParam("csrf", tokenId)
            .post(urlProps.getProperty("devVGWAuthorizeUrl"))
            .then()
            .assertThat()
            .statusCode(302)
            .extract().response();

    locationHeaderForAuthCode = response.getHeader("Location");

}

private static void getAccToken() throws JsonException {
    response = given()
            .log().all().config(RestAssured.config()
                    .encoderConfig(EncoderConfig.encoderConfig()
                            .encodeContentTypeAs("x-www-form-urlencoded",
                                    ContentType.URLENC)))
            .cookies(cookies)
            .header("Accept-Encoding", "gzip, deflate, br")
            .formParam("grant_type", "authorization_code")
            .formParam("client_id", userClientID)
            .formParam("client_secret", userClientSecrets)
            .formParam("redirect_uri", devRedirectUrl)
            .formParam("code", locationHeaderForAuthCode)
            .post(urlProps.getProperty("devVGWAccessTokenUrl"))
            .then()
//                .assertThat()
//                .statusCode(200)
            .log().all()
            .extract().response();

    accToken = response.jsonPath().getString("access_token");
    System.out.println("Access token is " + accToken);
}

您只需要 String 的簡單split()方法即可從此文本中提取值。

String url = "https://something.url.com/myapp/auth/token?code=**12WVUcPyFbmTZUOcgaluGl98r08**&iss=https%3A%2F%2Fsomething.url.com%3A8443%2Fam%2Foauth2%2Fvendors&client_id=5c1d7ex3-g3rc-4a64-9855-074a57cc1570";
String[] texts = url.split("code=");
String code = texts[1].split("&")[0];
System.out.println(code); //**12WVUcPyFbmTZUOcgaluGl98r08**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM