I am trying to validate the gmail API using restAssured. As per the docs, it requires authentication with Key and OAuth2.0. I used POSTMAN initially and was able to generate the access token and subsequently hit the request to get a success response. Now I want to achieve the same with Rest Assured framework.
I want to add the logic of token generation somewhere in beforeMethod/beforeTest in testNG framework.
I have basically two questions:
- How should I be setting up the API credentials for OAuth in Google Cloud Platform for hitting the request through Rest Assured(like We do for Postman)
- What should be the Request Endpoint and method.
So far I have tried below approaches referring to various solutions posted on Stack Overflow and other blogs:
Approach 1
public void oAuthToken() { Response res = given(). auth(). preemptive().basic("username", "password"). header("Content-Type","application/json"). queryParam("key","KeyGeneratedFromAPICedentials"). formParam("client_id","created an OAuth Client ID"). formParam("client_secret","created an OAuth Client Secret_ID"). formParam("grant_type","client_credentials"). when(). get("https://accounts.google.com/o/oauth2/auth"). //Getting this endpoint from JSON in OAuth Client ID created in google Cloud Platform then().assertThat().statusCode(200).extract().response(); System.out.println("This is the response : " +res.asString()); }
Result: Expected status code <200> but was <400>.
Approach 2:
public void oAuthToken() {
Response res = given().
auth().
preemptive().basic("username", "password").
header("Content-Type","application/json").
queryParam("key","KeyGeneratedFromAPICedentials").
formParam("client_id","created an OAuth Client ID").
formParam("client_secret","created an OAuth Client Secret_ID").
formParam("grant_type","client_credentials").
when().
get("https://oauth2.googleapis.com/token").
//Getting this endpoint from JSON in OAuth Client ID as Token_URI created in google Cloud Platform
then().assertThat().statusCode(200).extract().response();
System.out.println("This is the response : " +res.asString());
}
Result: Expected status code <200> but was <404>
Approach 3:
public void oAuthToken() {
RestAssured.baseURI="https://oauth2.googleapis.com";
Response res = given().
auth().preemptive().basic("Client_ID", "Client_Secret").
contentType("application/x-www-form-urlencoded").
formParam("grant_type","client_credentials").
formParam("scope","openid").
when().
get("/token").
then().assertThat().statusCode(200).extract().response();
System.out.println("This is the response : " +res.asString());
}
Result : Got 404 as response again.
Approach 4: Passed the access token directly after fetching it through "Generate Access Token" in postman.
Result: Got 403 as the response.
Needless to say to the experts here that I am quite new to Rest Assured and just trying to shoot arrows in the dark to get things working.
I want a robust approach of generating the OAuth token before running the Test each time. Feel free to guide me to any existing documentation as well.
This is the link to the API docs I am trying to access: https://developers.google.com/gmail/api/v1/reference/users/getProfile#auth