1
votes

For my API testing, I have been getting the Authentication Token with the help of Postman. API uses OAuth2.0 for authentication.

While using Postman, I am entering the details like Token Name, Grant Type, Access Token Url, Client ID, Client Secret, Scope, Client Authentication. Once clicked on the Request Token, Bearer token is received and is updated in the Header of the request. Please refer the image attached.Getting OAuth2.0 bearer token with Postman

But now, I want to do it with Rest Assured using Java. Hence I want to know the code how do I get the Bearer token by using Rest Assured?

1

1 Answers

0
votes

Assuming you are sending the client id secret and other details in the Query Parameters, for the GET and your response header access_token has the value you are looking.

public class Sample {
    String oauth_token ;    
    @BeforeTest
        public void sampletest() {
            oauth_token = RestAssured.given().auth().basic("username", "password").

                     .queryParams("client_id", "client_id_value")
                     .queryParams("client_secret", "client_secret_value")
                     .queryParams("grant_type", "authorization_code")
                     .queryParams("redirect_uri", "https://someuritoredirect")
                     .queryParams("otherparams", "sampleparam")
                   .when().get("https://uri.to.get.hostname.com/oauth2/access_token")
                     .then()
                     .statusCode(200)
                     .extract().header("access_token");
        }
}