The only way is to simulate web browser, i.e. HTML Unit. Haven't test the code, but I hope you get the idea
- Create HTMLUnit browserless class
public class HtmlUnit {
public void getLink(String url) throws IOException {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getCookieManager().setCookiesEnabled(true);
HtmlPage page = webClient.getPage(url);
final HtmlForm form = page.getForms().get(0);
// simulate login
.......
HtmlForm form = page.getForms().get(0);
// end login and get redirect to approval page
// simulate approval here
HtmlForm form = page.getForms().get(0);
final HtmlSubmitInput button = form.getInputByName("approve");
page = button.click();
page.getEnclosingWindow().getTopWindow().getEnclosedPage();
HtmlElement element = page.getFirstByXPath("//*[@id=\"content\"]/p/text()");
System.out.println(element.asText());
}
}
- Add HTMLUnit to get temporary token path
public String getAndAuthorizeTemporaryToken(String consumerKey, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
// others code here
authorizationURL.temporaryToken = response.token;
System.out.println("Retrieve request token. Go to " + authorizationURL.toString() + " to authorize it.");
HtmlUnit htmlUnit = new HtmlUnit();
htmlUnit.getLink(authorizationURL.toString());
return response.token;
}
You will get something like this
You have successfully authorized 'DOMAIN'. Your verification
code is 'VERIFICATION_CODE'. You will need to enter this exact text when
prompted. You should write this value down before closing the browser
window.
Use regex or string finder to get the verification code from it.
(Optional) If you don't want to simulate login (or using your browser cookie to Jira), try to save cookie from Jira and load it through HTML Unit.