1 year ago
#360100
DarkShadows
Rest Assured Bearer authentication GraphQL
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);
}
}
selenium
graphql
rest-assured
bearer-token
0 Answers
Your Answer