Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不解析JSON文档以返回访问令牌变量-Rest Assured/Java_Java_Json_Rest Assured_Jsonpath_Rest Assured Jsonpath - Fatal编程技术网

不解析JSON文档以返回访问令牌变量-Rest Assured/Java

不解析JSON文档以返回访问令牌变量-Rest Assured/Java,java,json,rest-assured,jsonpath,rest-assured-jsonpath,Java,Json,Rest Assured,Jsonpath,Rest Assured Jsonpath,我有一个步骤定义,用于执行基本身份验证以获取访问令牌。但是,当调用restassurextension类中的authenticateWithBasicAuth方法时,我无法检索字符串 我收到错误io.restasured.path.json.exception.JsonPathException:无法解析json文档 这是步骤定义: @And("I perform basic auth operation to get access token") public

我有一个步骤定义,用于执行基本身份验证以获取访问令牌。但是,当调用restassurextension类中的authenticateWithBasicAuth方法时,我无法检索字符串

我收到错误io.restasured.path.json.exception.JsonPathException:无法解析json文档

这是步骤定义:

  @And("I perform basic auth operation to get access token")
    public void i_Perform_Basic_Auth_Operation_To_Get_Access_Token() throws Throwable {

        String username = CommonTestData.consumerKey;
        String password = CommonTestData.consumerSecret;
        String firstParameterName = CommonTestData.basicAuthQueryParamsKey1Variable;
        String firstParameterValue = CommonTestData.basicAuthQueryParamsValue1Variable;
        String secondParameterName = CommonTestData.basicAuthQueryParamsKey2Variable;
        String secondParameterValue = CommonTestData.basicAuthQueryParamsValue2Variable;

        RestAssuredExtension restAssuredExtension = new RestAssuredExtension(APIConstant.ApiMethods.POST,null);
        token = restAssuredExtension.authenticateWithBasicAuth(username, password, firstParameterName, firstParameterValue, secondParameterName, secondParameterValue);

        System.out.println("access_token is " + token);
    }
这是通用的RestassureExtension类和方法:

  @And("I perform basic auth operation to get access token")
    public void i_Perform_Basic_Auth_Operation_To_Get_Access_Token() throws Throwable {

        String username = CommonTestData.consumerKey;
        String password = CommonTestData.consumerSecret;
        String firstParameterName = CommonTestData.basicAuthQueryParamsKey1Variable;
        String firstParameterValue = CommonTestData.basicAuthQueryParamsValue1Variable;
        String secondParameterName = CommonTestData.basicAuthQueryParamsKey2Variable;
        String secondParameterValue = CommonTestData.basicAuthQueryParamsValue2Variable;

        RestAssuredExtension restAssuredExtension = new RestAssuredExtension(APIConstant.ApiMethods.POST,null);
        token = restAssuredExtension.authenticateWithBasicAuth(username, password, firstParameterName, firstParameterValue, secondParameterName, secondParameterValue);

        System.out.println("access_token is " + token);
    }
我认为问题在于
返回executeAPI().getBody().jsonPath().getString(“访问令牌”)因为我无法解析JSON对象?这就是我被困的地方

public class RestAssuredExtension {

    private RequestSpecBuilder builder = new RequestSpecBuilder();
    private String method;
    private String url;

    /**
     * RestAssuredExtension constructor to pass the initial settings for the the following method
     * @param method
     * @param token
     */
    public RestAssuredExtension(String method, String token) {

        //Formulate the API url
        this.url = "https://api.business.govt.nz/services/token";
        this.method = method;

        if(token != null)
            builder.addHeader("Authorization", "Bearer " + token);
    }

    /**
     * ExecuteAPI to execute the API for GET/POST/DELETE
     * @return ResponseOptions<Response>
     */
    private ResponseOptions<Response> executeAPI() {
        RequestSpecification requestSpecification = builder.build();
        RequestSpecification request = RestAssured.given();
        request.contentType(ContentType.JSON);
        request.spec(requestSpecification);

        if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.POST))
            return request.post(this.url);
        else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.DELETE))
            return request.delete(this.url);
        else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.GET))
            return request.get(this.url);
        else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.PUT))
            return request.get(this.url);
        return null;
    }


    /**
     * Authenticate to get the token variable
     * @return string token
     */
    public String authenticateWithBasicAuth(String username, String password, String firstParameterName, String firstParameterValue, String secondParameterName, String secondParameterValue){
        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue);
        return executeAPI().getBody().jsonPath().getString("access_token");
    }
公共类重新启动扩展{
私有RequestSpecBuilder=新RequestSpecBuilder();
私有字符串方法;
私有字符串url;
/**
*重新启动AssureExtension构造函数以传递以下方法的初始设置
*@param方法
*@param令牌
*/
公共重新启动扩展(字符串方法、字符串标记){
//制定API url
this.url=”https://api.business.govt.nz/services/token";
这个方法=方法;
if(令牌!=null)
builder.addHeader(“授权”、“持有人”+令牌);
}
/**
*ExecuteAPI执行GET/POST/DELETE的API
*@返回响应选项
*/
private ResponseOptions executeAPI(){
RequestSpecification RequestSpecification=builder.build();
RequestSpecification request=restasured.given();
contentType(contentType.JSON);
request.spec(requestSpecification);
if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.POST))
返回request.post(this.url);
else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.DELETE))
返回请求.delete(this.url);
else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.GET))
returnrequest.get(this.url);
else if(this.method.equalsIgnoreCase(APIConstant.ApiMethods.PUT))
returnrequest.get(this.url);
返回null;
}
/**
*验证以获取令牌变量
*@返回字符串令牌
*/
公共字符串authenticateWithBasicAuth(字符串用户名、字符串密码、字符串firstParameterName、字符串firstParameterValue、字符串secondParameterName、字符串secondParameterValue){
builder.setAuth(RestAssured.preemptive().basic(用户名、密码)).addQueryParam(firstParameterName,firstParameterValue).addQueryParam(secondParameterName,secondParameterValue);
返回executeAPI().getBody().jsonPath().getString(“访问令牌”);
}

在与朋友调试了上述内容几次之后,我们发现问题在于我们明确指定了ContentType.JSON作为内容类型

因此,为了确保能够成功运行测试,我们从executeAPI方法中注释掉了行request.contentType(contentType.JSON),并将内容类型作为addHeader中的头添加到authenticateWithBasicAuth方法中:

/**
     * Authenticate to get the token variable
     * @return string token
     */
    public String authenticateWithBasicAuth(String username, String password, String firstParameterName, String firstParameterValue, String secondParameterName, String secondParameterValue){
//        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue).addHeader("Content-Type", "application/x-www-form-urlencoded");
        builder.setAuth(RestAssured.preemptive().basic(username, password)).addQueryParam(firstParameterName, firstParameterValue).addQueryParam(secondParameterName, secondParameterValue);
        String token = executeAPI().getBody().jsonPath().getString("access_token");
        return executeAPI().getBody().jsonPath().getString("access_token");
    }

然后我再次运行它,没有添加内容类型标题,它工作正常,我能够以字符串形式获取访问令牌。

@fakataha您对此有什么建议吗?或者@Kartmcad有什么建议吗?