Salesforce API Login Authentication

package com.thysmichels;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.partner.*;
import com.sforce.ws.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class SalesforceLogin {

	/**
	 * @param args
	 */
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try
		{
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter Salesforce Username: ");
			String username = reader.readLine();
			System.out.println("Enter Salesforce Password: ");
			String password = reader.readLine();
			System.out.println("Enter Salesforce Security Token: ");
			String sectoken = reader.readLine();
			if (SFLogin(username, password, sectoken) == true)
			{
				System.out.println("*** Login Success ***");
			}
			else
			{
				System.out.println("*** Login Failure ***");
			}
		}
		catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		}
		
	}
	public static boolean SFLogin(String username, String password, String sectoken)
	{
		
		try {
			
			ConnectorConfig sfconfig = new ConnectorConfig();
			//Use your salesforce username = email
		    sfconfig.setUsername(username);
		    //Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
		    sfconfig.setPassword(password + sectoken);
		    EnterpriseConnection entercon = null;
		    sfconfig.setAuthEndpoint("https://login.salesforce.com/services/Soap/c/24.0");
		    entercon = new EnterpriseConnection(sfconfig);
		    if (entercon.login(username, password + sectoken).isPasswordExpired()== false)
		    {
		    	System.out.println("SessionID: " + sfconfig.getSessionId());
		    	System.out.println("Auth End Point: " + sfconfig.getAuthEndpoint());
		    	System.out.println("Service End Point: " + sfconfig.getServiceEndpoint());
		    	entercon.logout();
		    	return true;
		    }
		    else
		    {
		    	return false;
		    }
		   
		}
		   catch (ConnectionException ce) {

			      return false;
		   }
	}
}

Result

SessionID: 00DA0000000ao2F!ASAAQK5JbvhkfzbsYOG7_c9XFKauUzzw9HjB8IJ8r9W54IapCOmQk8AdK59u_O.Izd2hDtyXCDqbltlryV.MI3DKSagoIiUR
Auth End Point: https://login.salesforce.com/services/Soap/c/24.0
Service End Point: https://na10-api.salesforce.com/services/Soap/c/24.0/00DA0000000ao2F
*** Login Success ***
Advertisements

2 Comments on “Salesforce API Login Authentication

  1. I getting error at import com.sforce.soap.enterprise
    Where to get jar file. I am not able to find exactly

  2. If you are interested in topic: can you make money
    growing vegetables – you should read about Bucksflooder first

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: