RestAssured 和 Bearer 令牌

RestAssured and Bearer token

对 RestAssured 和一般身份验证非常陌生。

我有一个 API 我可以使用客户端凭据在 Postman 中发出获取请求。它 returns 不记名令牌。

我现在正在尝试让它与黄瓜一起使用并放心。

所以我们有这段代码,它试图为下一步 API 调用获取不记名令牌。

@Given("^the user has a valid authentication token for credentials$")
public void the_user_with_credentials_something_and_something_has_a_valid_authentication_token() throws Throwable {
    
        RestAssured.baseURI=SCN_CONTEXT.getProp().getExternalUri();
        Response accessTokenResponse = given()
                 .header("Content-Type", "application/json")
                 .queryParams("client_id", SCN_CONTEXT.getProp().getClientId())
                 .queryParams("client_secret", SCN_CONTEXT.getProp().getClientSecret())
                 .when().log().all().get(APIResources.valueOf("oAuthAPI").getValue());
         
         System.out.println(accessTokenResponse.getStatusCode());
         SCN_CONTEXT.get_SCN().write("accessTokenResponse: " + accessTokenResponse.getStatusCode());

          JsonPath js=new io.restassured.path.json.JsonPath(accessTokenResponse.body().asString());
            id_token="Bearer " + js.getString("id_token");
            SCN_CONTEXT.setId_token(id_token);
            SCN_CONTEXT._SCN.write("Authentication Token is: " + id_token);
            
    }

使用的externalUri是我在Postman中使用的token访问uri

但是在这样做时我得到了 403 并且令牌为空

控制台日志:(有什么?我删除了)

Request method: GET
Request URI:    https://?.com/?/v1/?/oauth/token?client_id=?&client_secret=?
Proxy:          <none>
Request params: <none>
Query params:   client_id=?
                client_secret=?
Form params:    <none>
Path params:    <none>
Headers:        Accept=*/*
                Content-Type=application/json; charset=UTF-8
Cookies:        <none>
Multiparts:     <none>
Body:           <none>
403

这里有很多要看的东西,对于类似的要求,我在 POSTMAN 控制台中有以下内容。表面上这可能是一个 GET 调用,但从技术上讲这是一个 POST 调用

授权是 Base64 编码,作为 header 发送,查询参数是 grant_typescope

对此的放心代码将是

given().header("Content-Type", "application/x-www-form-urlencoded").auth().preemptive()
        .basic("abc", "def")
        .queryParam("grant_type", "client_credentials").queryParam("scope", "123").when()
        .post("ghi").then().extract().response();