Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login with Facebook in java From Desktop Removing Permission box

After a lot of doing searching of login with facebook from desktop application i made my my self using DJ Native swing browser.

but still i have one problem that i need to change something.

I am using login with facebook using Enabled Browser Swing. When I logged into Facebook I am getting one permission box which is asking for user permission. but I don't want to show that box. is it possible to grant permission auto in Facebook.

I just want to remove this warning dialog and want to grant Permission auto so how could I do that?

any Suggestion is Welcomed.

like image 976
Kishan Bheemajiyani Avatar asked May 02 '14 10:05

Kishan Bheemajiyani


1 Answers

Well i solved it by just removing some parameters like i make URL of

 public static String firstRequest = "https://graph.facebook.com/oauth/authorize?"
            + "client_id="
            + API_KEY
            + "&redirect_uri=http://www.facebook.com/connect/login_success.html&"
            + "scope=email,offline_access";

its just asking only once as i want :)

well here the example for the same.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.SimpleWebBrowserExample;

import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.StringReader;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

/**
 *
 * @author kishan
 */
public class FacBookApiLogin extends javax.swing.JFrame {

    public static String API_KEY = "*****************";
    public static String SECRET = "********************";

    public static String firstRequest = "https://graph.facebook.com/oauth/authorize?"
            + "client_id="
            + API_KEY
            + "&redirect_uri=http://www.facebook.com/connect/login_success.html&"
            + "scope=email,offline_access";

    //
    public static String secondRequest = "https://graph.facebook.com/oauth/access_token?"
            + "client_id="
            + API_KEY
            + "&redirect_uri=http://www.facebook.com/connect/login_success.html&"
            + "client_secret=" + SECRET + "&code=";

    public static String access_token = "";
    public static boolean firstRequestDone = false;
    public static boolean secondRequestDone = false;
    static FacBookApiLogin apiLogin;
    static SimpleFrame browserExample;

    /**
     * Creates new form FacBookApiLogin
     */
    public FacBookApiLogin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        LoginFacebook = new javax.swing.JButton();
        AutoLoginButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        LoginFacebook.setText("Login Facebook");
        LoginFacebook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginFacebookActionPerformed(evt);
            }
        });

        AutoLoginButton.setText("Auto Login");
        AutoLoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AutoLoginButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(AutoLoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LoginFacebook)
                .addContainerGap(109, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 247, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LoginFacebook)
                    .addComponent(AutoLoginButton)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void LoginFacebookActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        NativeInterface.open();
        final JFrame authFrame = new JFrame();
        // Create the JWebBrowser and add the WebBrowserAdapter
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        final JWebBrowser webBrowser = new JWebBrowser();
        webBrowser.navigate(firstRequest);
        webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
            @Override
            public void locationChanged(WebBrowserNavigationEvent e) {
                super.locationChanged(e);
                // Check if first request was not done
                if (!firstRequestDone) {
                    // Check if you left the location and were redirected to the next 
                    // location
                    if (e.getNewResourceLocation().contains("http://www.facebook.com/connect/login_success.html?code=")) {
                        // If it successfully redirects you, get the verification code
                        // and go for a second request
                        String[] splits = e.getNewResourceLocation().split("=");
                        String stage2temp = secondRequest + splits[1];
                        System.err.println("URL location" + stage2temp);
                        webBrowser.navigate(stage2temp);
                        System.out.println("This is what we looking for" + splits[2]);

                        firstRequestDone = true;
                        if (firstRequestDone == true) {
                            //apiLogin.dispose();
                            //browserExample = new SimpleFrame();
                            //browserExample.setVisible(true);
                        }

                    }
                } else {
                    // If secondRequest is not done yet, you perform this and get the 
                    // access_token
                    if (!secondRequestDone) {
                        System.out.println(webBrowser.getHTMLContent());
                        // Create reader with the html content
                        StringReader readerSTR = new StringReader(webBrowser.getHTMLContent());
                        // Create a callback for html parser
                        HTMLEditorKit.ParserCallback callback
                                = new HTMLEditorKit.ParserCallback() {
                                    public void handleText(char[] data, int pos) {
                                        System.out.println(data);
                                        // because there is only one line with the access_token 
                                        // in the html content you can parse it.
                                        String string = new String(data);

                                        System.out.println("Main string : " + data.toString());
                                        String[] temp1 = string.split("&");

                                        String[] temp2 = temp1[0].split("=");

                                        access_token = temp2[1];
                                        System.out.println("accesstoken is here" + access_token);

                                        System.out.println("length of token" + temp2.length);
                                        System.err.println("Temp 0" + temp2[0]);
                                    }
                                };
                        try {
                            // Call the parse method 
                            new ParserDelegator().parse(readerSTR, callback, false);

                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        // After everything is done, you can dispose the jframe
                        authFrame.dispose();

                    }
                }

            }
        });
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        authFrame.add(webBrowserPanel);
        authFrame.setSize(400, 500);
        authFrame.setVisible(true);

    }                                             

    private void AutoLoginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        NativeInterface.open();
        final JFrame loginFrame = new JFrame();
        JPanel webBrowserPanel = new JPanel(new BorderLayout());
        // this is the JWebBrowser i mentioned earlier
        final JWebBrowser webBrowser = new JWebBrowser();
        // You can set this fields to false, or even let them activated
        webBrowser.setMenuBarVisible(false);
        webBrowser.setButtonBarVisible(false);
        webBrowser.setLocationBarVisible(false);
        final String fb_url = "http://www.facebook.com/";
        webBrowser.navigate(fb_url);

        // Here we add to our JWebBrowser an Adapter and override the 
        // locationChanging() method. Here we can check, if we are 
        // changing the location
        // in our case the String fb_url, then this JWebBrowser can be 
        // disposed.
        // The Timer is set for 2 seconds, so we can still see if the 
        // login was successfull or not.
        webBrowser.addWebBrowserListener(new WebBrowserAdapter() {

            @Override
            public void locationChanging(WebBrowserNavigationEvent e) {
                super.locationChanging(e);

                System.out.println(e.getNewResourceLocation());

                if (!e.getNewResourceLocation().equals(fb_url)) {
                    Timer timer = new Timer(2000, new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                            loginFrame.dispose();
                        }
                    });
                    timer.start();
                }
            }
        });

        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
        loginFrame.add(webBrowserPanel);
        loginFrame.setSize(400, 500);
        loginFrame.setVisible(true);
    }                                               

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;

                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FacBookApiLogin.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FacBookApiLogin.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FacBookApiLogin.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FacBookApiLogin.class
                    .getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                apiLogin = new FacBookApiLogin();
                apiLogin.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton AutoLoginButton;
    private javax.swing.JButton LoginFacebook;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}
like image 95
Kishan Bheemajiyani Avatar answered Nov 14 '22 21:11

Kishan Bheemajiyani