RestAssured Oauth2 token generation code.
Actually its of two steps.
1) login in web using authorize url and obtain code
2) using code and other attributes generate token.
Selenium code to obtain code from Authorize url
public void getToken() {
System.setProperty("webdriver.chrome.driver",
"path of chrome");
WebDriver driver = new ChromeDriver();
String scope="#scope#";
//please use static declarations for the url, clientid, redirecturi
System.out.println(""+url+"?scope="+scope+"&auth_url="+url+"&client_id="+client_id+"&response_type=code&redirect_uri="+call_back+"");
driver.get(
""+url+"?scope="+scope+"&auth_url="+url+"&client_id="+client_id+"&response_type=code&redirect_uri="+call_back+"");
driver.findElement(By.id("UserName")).sendKeys("#user#");
driver.findElement(By.id("Passwd")).sendKeys("#password#");
driver.findElement(By.xpath("//*[@id=\'\']/div[4]/button")).click(); - LOGIN Button
//Capture code from the URL
String token_url = driver.getCurrentUrl();
String[] code1 = token_url.split("code=");
code = code1[1];
System.out.println(code);
driver.close();
}
Actually its of two steps.
1) login in web using authorize url and obtain code
2) using code and other attributes generate token.
Selenium code to obtain code from Authorize url
public void getToken() {
System.setProperty("webdriver.chrome.driver",
"path of chrome");
WebDriver driver = new ChromeDriver();
String scope="#scope#";
//please use static declarations for the url, clientid, redirecturi
System.out.println(""+url+"?scope="+scope+"&auth_url="+url+"&client_id="+client_id+"&response_type=code&redirect_uri="+call_back+"");
driver.get(
""+url+"?scope="+scope+"&auth_url="+url+"&client_id="+client_id+"&response_type=code&redirect_uri="+call_back+"");
driver.findElement(By.id("UserName")).sendKeys("#user#");
driver.findElement(By.id("Passwd")).sendKeys("#password#");
driver.findElement(By.xpath("//*[@id=\'\']/div[4]/button")).click(); - LOGIN Button
//Capture code from the URL
String token_url = driver.getCurrentUrl();
String[] code1 = token_url.split("code=");
code = code1[1];
System.out.println(code);
driver.close();
}
Generate token from token using code
public void token() {
baseURI = "baseurl";
String test = "code=" + code
+ "&client_id="+client_id+"&client_secret="+client_secret+"&grant_type=authorization_code&redirect_uri="+call_back+"";
String response = given().urlEncodingEnabled(false).contentType(ContentType.URLENC).body(test).when().log()
.all().post("/oauth/token").asString();
JsonPath js = new JsonPath(response);
access_token = js.getString("access_token");
System.out.println(access_token);
}
Reacties
Een reactie posten