休息保证的携带者身份验证graphql

发布于 2025-01-18 06:42:14 字数 2397 浏览 1 评论 0原文

我是新手的,可以放心并坚持使用Bearer身份验证方案,在该方案中,我我的GraphQl查询需要首先生成令牌,然后通过它通过,然后将其通过以获取响应结果。 我创建了以下脚本,但是它生成了令牌,但由于我遇到“错误”时未能在第二个文章请求中返回响应:“携带者令牌畸形”

public class Auth {
    
    @Test
    public void bearerToken() {
        RestAssured.baseURI="https://xyzapi.google.com";
        RequestSpecification request=RestAssured.given();
        JSONObject requestParams = new JSONObject();
        
        requestParams.put("grant_type", "client_credentials");
        requestParams.put("client_id", "fa3f2b6a-3f0b-44b4-a084-ceed3");    
        requestParams.put("client_secret", "WeLzkxgYSrfH3wXTqjuvDoFEN04aV86Osd1UKl9Z52RPh");
        request.header("Content-Type","application/json");
        request.header("Authorization","Basic ZmEzZjJiNmEtM2YwYi00NGI0LWEwODQtY2VlZGVhYjMwNDgzOldlTHpreGdZWODZHUW10QkF5aW43TUNKYnBJY09zZDFVS2w5WjUyUlBo");
        
        request.body(requestParams.toJSONString());
        Response responseFromGeneratedToken  = request.request(Method.POST,"/oauth/token");
        responseFromGeneratedToken.prettyPrint();
        
        
        String jsonString =responseFromGeneratedToken.getBody().asString();
        String tokenGenerated =JsonPath.from(jsonString).get("access_token");
        
        System.out.println("The generated token value is :"+tokenGenerated);
        request.header("Authorization","Bearer "+tokenGenerated);
        request.header("Content-Type","application/json");
        JSONObject requestParams1 = new JSONObject();
        requestParams1.put("query MyQuery","{\r\n"
                + "  Get(EmployeeId: \"\", offset: 0, limit: 2, EmployerId: \"\") {\r\n"
                + "    xyz{\r\n"
                + "      Number\r\n"
                + "      Code\r\n"
                + "      ner\r\n"
                + "      om {\r\n"
                + "       Ratings {\r\n"
                + "          ted\r\n"
                + "          ore\r\n"
                + "        }\r\n"
                + "      }\r\n"
                + "}\r\n"
                + "");
                
    
        request.header("Content-Type","application/json");
        request.body(requestParams1.toJSONString());
        Response Responsefinal=request.request(Method.POST,"/lists/SeniorCitizen/");
        String Responsebody = Responsefinal.getBody().asString();
        System.out.println(Responsebody);
            
        }
    }

I'm new to learn rest assured and stuck with bearer authentication scenario where i've graphql query and need to pass by generating token first and then pass it through in order to get response result.
I've created following scripts however its generating the token but failed to return the response in second post request as i'm getting "error": "Bearer token malformed"

public class Auth {
    
    @Test
    public void bearerToken() {
        RestAssured.baseURI="https://xyzapi.google.com";
        RequestSpecification request=RestAssured.given();
        JSONObject requestParams = new JSONObject();
        
        requestParams.put("grant_type", "client_credentials");
        requestParams.put("client_id", "fa3f2b6a-3f0b-44b4-a084-ceed3");    
        requestParams.put("client_secret", "WeLzkxgYSrfH3wXTqjuvDoFEN04aV86Osd1UKl9Z52RPh");
        request.header("Content-Type","application/json");
        request.header("Authorization","Basic ZmEzZjJiNmEtM2YwYi00NGI0LWEwODQtY2VlZGVhYjMwNDgzOldlTHpreGdZWODZHUW10QkF5aW43TUNKYnBJY09zZDFVS2w5WjUyUlBo");
        
        request.body(requestParams.toJSONString());
        Response responseFromGeneratedToken  = request.request(Method.POST,"/oauth/token");
        responseFromGeneratedToken.prettyPrint();
        
        
        String jsonString =responseFromGeneratedToken.getBody().asString();
        String tokenGenerated =JsonPath.from(jsonString).get("access_token");
        
        System.out.println("The generated token value is :"+tokenGenerated);
        request.header("Authorization","Bearer "+tokenGenerated);
        request.header("Content-Type","application/json");
        JSONObject requestParams1 = new JSONObject();
        requestParams1.put("query MyQuery","{\r\n"
                + "  Get(EmployeeId: \"\", offset: 0, limit: 2, EmployerId: \"\") {\r\n"
                + "    xyz{\r\n"
                + "      Number\r\n"
                + "      Code\r\n"
                + "      ner\r\n"
                + "      om {\r\n"
                + "       Ratings {\r\n"
                + "          ted\r\n"
                + "          ore\r\n"
                + "        }\r\n"
                + "      }\r\n"
                + "}\r\n"
                + "");
                
    
        request.header("Content-Type","application/json");
        request.body(requestParams1.toJSONString());
        Response Responsefinal=request.request(Method.POST,"/lists/SeniorCitizen/");
        String Responsebody = Responsefinal.getBody().asString();
        System.out.println(Responsebody);
            
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文