2

Could someone please guide us how can we implement the auth flow for our app to be able to login to any Salesforce org and not be tied up to a specific org ?

I am interested to implement same aithentication flow used in Dataloader (I think it's called Oauth). Where the user will be provided with web page with Salesforce page to login with his username and password.

Thanks.

2

Basically, you just pop up a WebView with the appropriate User-Agent Flow endpoint, and set an event hook for when the callback URL is triggered. Once that happens, you'll have an access token you can use for all applicable APIs (Bulk, Metadata, SOAP, REST, etc). The user can select a custom domain by using the custom domain link that appears on the login page. I don't have any example code, but I'll see if I can find something you could use.


Here's an example code that I wrote personally that gets as far as getting the data into a HashMap. This version is Java 8, because I just wanted to use lambdas for brevity. What you do with this is up to you. Ideally, you'd actually probably open up a new window, let the user go through the flow, then dispose of the frame when you're done. This is a simple standalone app.

In this example code, it is assumed that the Connected App has a callback URI of callout://result. Adjust this code or your Connected App accordingly. I recommend not using http or https so that you don't have to implement your own URLStreamHandler for loading the actual OAuth pages.

package fear.brian.oauthdemo;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLStreamHandler;
import java.util.HashMap;

import javax.swing.JFrame;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;

@SuppressWarnings("serial")
public class Main extends JFrame {

    public Main self;
    public JFXPanel panel;

    public Main() {
        self = this;
        setTitle("Demo");
        setBounds(new Rectangle(new Point(0, 0), new Dimension(400, 800)));
        panel = new JFXPanel();
        this.add(panel);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Platform.runLater(() -> {
            WebView view = new WebView();
            panel.setScene(new Scene(view));
            URL.setURLStreamHandlerFactory((protocol) -> {
                if(protocol.equals("callout")) {
                    return new URLStreamHandler() {
                        @Override
                        protected URLConnection openConnection(URL arg0) throws IOException {
                            return new URLConnection(arg0) {
                                @Override
                                public void connect() throws IOException {
                                    HashMap<String, String> data = new HashMap<String, String>();
                                    String[] parts = arg0.toExternalForm().split("#");
                                    String[] keypairs = parts[1].split("&");
                                    for(String keypair: keypairs) {
                                        String[] keypairparts = keypair.split("=",2);
                                        data.put(keypairparts[0], URLDecoder.decode(keypairparts[1], "UTF-8"));
                                    }
            /***********************************************************************************
            At this point, data contains access_token you can use to log in with, optional
            refresh_token if specified in the scope, API url (instance_url), token_type, and
            scope.
            ***********************************************************************************/
                                    System.out.println(data.toString());
                                }
                            };
                        }
                    };
                }
                return null;
            });
            view.getEngine().load(
                "https://login.salesforce.com/services/oauth2/authorize" +
                "?response_type=token" +
                "&client_id=" + "YOUR_CLIENT_ID_GOES_HERE" +
                "&redirect_uri=callout%3A%2F%2Fresult"
            );
        });

    }
    public static void main(String[] args) {
        new Main();
    }
}
  • Hi sfdcfox. 10x for answering. So basically I dont need to use the connected app and consumer secret and all these configurations right ? As we dont want to be dependent on a specific org. – sfdcdev Mar 27 '17 at 21:16
  • Ok I need to read about it more. So you say I am using my personal developer org to make this app work on any other org without any configurations. Please share if you find some java code. 10x a lot again !! – sfdcdev Mar 27 '17 at 21:26
  • @sfscfox one last question.. what should be the callback url ? it is a desktop app so I am not sure .. – sfdcdev Mar 27 '17 at 21:39
  • @sfdcdev Added a working example that you can use pretty much out of the box. If you're using Eclipse, you need to enable JavaFX under Project > Properties > Java Build Path > JRE System Library > Access Rules > Edit > Add > Resolution: Acessible, Rule Pattern: javafx/**. I tested this out before posting. I hope you find this useful. – sfdcfox Mar 28 '17 at 2:14
  • I really appreciate your help. hope you'll continue to contribute to this community. thank you !!! – sfdcdev Mar 28 '17 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.