0

I made a simple program to login to the instagram web site.

My original code works in a java main class but now i stumbled with something strange, when i'm putting the code, i'm using it in another class, so i can load and use it in other classes from a different package, doesn't access to the site anymore.

I just made a few trivial changes to the code, like receive a few parameters (like the username and password).

The original code from the main class:

WebClient webClient = new WebClient(BrowserVersion.CHROME);

        //configuracion de opciones de webbrowser
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getCookieManager().setCookiesEnabled(true);

        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        HtmlPage page = (HtmlPage) webClient.getPage(authorizationUrl);
        WebResponse response = page.getWebResponse();

        response.cleanUp(); 



       final HtmlForm loginForm = page.getForms().get(0);

     // revisar si la sesion ha expirado
       if(loginForm!=null){ //la session ha expirado relogear

              //se pone el usuario en el input html por nombre
              final HtmlTextInput txtUser = loginForm.getInputByName(usuarioInputName);
              txtUser.setValueAttribute("oestedev");

              //se pone el password en el input html por nombre
              final HtmlPasswordInput txtpass = loginForm.getInputByName(passwordInputName);
              txtpass.setValueAttribute("oeste18");

              //boton submit por valor
             final HtmlSubmitInput submitLogin = loginForm.getInputByValue(submitLoginButtonValue);

             final HtmlPage returnPage = submitLogin.click(); 

             System.out.println( returnPage.getUrl());


             String urlAcceso = returnPage.getUrl().toString();
             String[] parteUrl = urlAcceso.split("=");
            // String urlAuth = parteUrl[0]; // url de autorizacion
             codigo = parteUrl[1]; // codigo para obtener el access token

             System.out.println(codigo);



        }


        webClient.close();

from the class i use as library:

public void realizarConexion(String usuario,String password) throws IOException{

        String authorizationUrl = service.getAuthorizationUrl(TOKEN_ACCESO);
        String codigo = null;
        WebClient webClient = new WebClient(BrowserVersion.CHROME);

        //configuracion de opciones de webbrowser
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setUseInsecureSSL(true);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getCookieManager().setCookiesEnabled(true);

            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setRedirectEnabled(true);

            //acceso a la url desde webclient


                  HtmlPage page = (HtmlPage) webClient.getPage(authorizationUrl);
                  WebResponse response = page.getWebResponse();

                    response.cleanUp(); 

                   final HtmlForm loginForm = page.getForms().get(0);

                   // try to login to the site
                   if(loginForm!=null){ //la session ha expirado relogear

                          //se pone el usuario en el input html por nombre
                            HtmlTextInput txtUser = loginForm.getInputByName(usuarioInputName);
                          txtUser.setValueAttribute(usuario);

                          //se pone el password en el input html por nombre
                            HtmlPasswordInput txtpass = loginForm.getInputByName(passwordInputName);
                          txtpass.setValueAttribute(password);

                          //boton submit por valor
                           HtmlSubmitInput submitLogin = loginForm.getInputByValue(submitLoginButtonValue);

                         //accede al sitio
                          HtmlPage returnPage = submitLogin.click();      

                         String urlAcceso = returnPage.getUrl().toString();
                         System.out.println(urlAcceso);// to check if i got to the correct url
                         String[] parteUrl = urlAcceso.split("=");

                         codigo = parteUrl[1]; // codigo para obtener el access token
                   }

                Verifier verifier = new Verifier(codigo);
                Token accessToken = service.getAccessToken(TOKEN_ACCESO, verifier);

                instagram = new Instagram(accessToken);

                webClient.close();

    }
Abhiram mishra
  • 1,597
  • 2
  • 14
  • 34
Progs
  • 1,059
  • 7
  • 27
  • 63

0 Answers0