Using OAuth
Salesforce supports OAuth 2.0 authentication, letting you access data securely without having to reveal user name and password credentials.
You can configure the connector to use an Access Token or a Refresh Token to establish an OAuth connection.
To connect using an Access Token, you must specify the following connection properties:
AccessToken
Endpoint
To connect using a Refresh Token, you must specify the following connection properties:
RefreshToken
ClientID
ClientSecret
Endpoint
You provide the configuration information to the connector in the connection URL. For more information about the syntax of the connection URL, see Building the Connection URL.
Before setting the connector connection properties, you must configure your application to integrate with Salesforce and then obtain the necessary values for the connector connection properties:
- In your application, select an appropriate OAuth 2.0 authentication flow that Salesforce supports.
- In the Force.com administration console, configure a connected app for your application and obtain a Consumer Key and Consumer Secret, which is also known as a Client ID and Client Secret.
- Using the Consumer Key and any other required parameters, obtain the instance URL (to use as part of the
Endpoint
setting) and the Access Token or Refresh Token. Depending on the authentication flow that you selected, you might also need to obtain an authorization code.Note:If you are connecting to a sandbox system, you need to set the
Endpoint
to the sandbox URL instead.
For detailed information about setting up authorization using OAuth 2.0, see "Set Up Authorization" in the Force.com REST API Developer Guide: http://www.salesforce.com/us/developer/docs/api_rest/Content/quickstart_oauth.htm.
Example Using an Access Token
The following is an example connection URL for connecting to Salesforce using an Access Token:
jdbc:salesforce://localhost;AccessToken=c6ssMR0.0AJ1iOkXV5040A3km1JfJ0yloo03ylaWDdYS06aWE0xDA56JCf9hqomYIF1c3VKIH3;OAuthUrl=https://na11.salesforce.com/services/oauth2/token;Endpoint=https://na11.salesforce.com/services/Soap/u/48.0
The following code demonstrates how to set connection properties to use an Access Token in establishing an OAuth connection:
// --- DEFINE PROPERTIES FOR THE CONNECTION ---
Properties ConnectionProperties = new Properties();
// Set a variable for the Access Token received from
// Salesforce (using the Web Server Authentication Flow).
String AccessToken = "c6ssMR0.0AJ1iOkXV5040A3km1JfJ0yloo03ylaWDdYS06aWE0xDA56JCf9hqomYIF1c3VKIH3";
// The Endpoint property contains the Instance URL
// received from Salesforce. The endpoint should use the
// following form:
// https://[Instance].salesforce.com/services/Soap/u/48.0
String InstanceURL = "https://na11.salesforce.com";
String ServiceEndpoint = InstanceURL + "/services/Soap/u/48.0";
ConnectionProperties.setProperty("AccessToken", AccessToken);
ConnectionProperties.setProperty("Endpoint", ServiceEndpoint);
// --- END PROPERTIES ---
Example Using a Refresh Token
The following is an example connection URL for connecting to Salesforce using a Refresh Token:
jdbc:salesforce://localhost;RefreshToken=8qLdDkL1nqDpYqN4tRUCDO2.4fsMIYYsIn_MCPt.i3;ClientId=1KLcQce8MYQA9zLUnqEoSAo
ScsS_JPXL_szVVqzLPm3HQUFIbV_u8YxZZBbL8F;ClientSecret=e8MYQA9zLUnqEoSAu30GGvAUR5kHWZ7l0t;OAuthUrl=https://
na11.salesforce.com/services/oauth2/token;Endpoint=https://na11.salesforce.com/services/Soap/u/48.0
The following code demonstrates how to set connection properties to use a Refresh Token in establishing an OAuth connection:
// --- DEFINE PROPERTIES FOR THE CONNECTION ---
Properties ConnectionProperties = new Properties();
// Set a variable for the Refresh Token received from
// Salesforce (using the Web Server Authentication Flow).
String RefreshToken = "8qLdDkL1nqDpYqN4tRUCDO2.4fsMIYYsIn_MCPt.i3";
// Set a variable for the Access Token received from
// Salesforce (using Web Server Authentication Flow).
// Setting an Access Token is optional when using a
// Refresh Token.
// The connector uses the Refresh Token to get a new Access
// Token whenever the Access Token expires.
String AccessToken = "xZ.iKtGi0ztu30GGvAUR5kHWZ7l0t.5WGW5IGkg5y7W5.VJJNtwIOxXT.fDAOYDG3vyDu.AT7Di3";
// The ClientId is the "Consumer Key" that Salesforce
// registers for the application.
String ClientId = "1KLcQce8MYQA9zLUnqEoSAoScsS_JPXL_szVVqzLPm3HQUFIbV_u8YxZZBbL8F";
// The ClientSecret is the "Consumer Secret" that Salesforce
// registers for the application.
String ClientSecret = "e8MYQA9zLUnqEoSAu30GGvAUR5kHWZ7l0t";
// The Endpoint property contains the Instance URL
// received from Salesforce. The Endpoint should use
// the following form:
// https://[Instance].salesforce.com/services/Soap/u/48.0
String InstanceURL = "https://na11.salesforce.com";
String ServiceEndpoint = InstanceURL + "/services/Soap/u/48.0";
ConnectionProperties.setProperty("RefreshToken", RefreshToken);
// Setting an Access Token is optional when using a
// Refresh Token.
ConnectionProperties.setProperty("AccessToken", AccessToken);
ConnectionProperties.setProperty("ClientId", ClientId);
ConnectionProperties.setProperty("ClientSecret", ClientSecret);
ConnectionProperties.setProperty("Endpoint", ServiceEndpoint);
// --- END PROPERTIES ---