简体   繁体   中英

Using HtmlUnit to get the access token from instagram

Since httpURLconnection didn't cut out, i switched to htmlUnit to get programatically the auth code to get the access token from instagram and then do whatever i need from there, the thing is i'm stuck trying to retrieve the authorization code from the url

mysite.com/?code=ca1ec5b06a0b409293cff74ed9876a46

but i can't access to that link since it doesn't seems to be redirected from the authorization URL. this one:

https://instagram.com/accounts/login/?force_classic_login=&next=/oauth/authorize/%3Fclient_id%CLIENT_ID%26redirect_uri%3Dhttp%3A//MYSITE.COM%26response_type%3Dcode

this is my code where i try to access to that url:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setCssEnabled(true);     
    HtmlPage page = (HtmlPage) webClient.getPage(authURL);
    WebResponse response = page.getWebResponse();
    String content = response.getContentAsString();
    System.out.println(page.getUrl());

我通过尝试获取未登录到站点的授权URL来解决此问题,然后当我被要求登录时,我使用htmlunit获取html表单登录到instagram ,最后我可以使用代码获取所需的重定向URL。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM