-
Notifications
You must be signed in to change notification settings - Fork 38
Example Usage
Here is a sample flow for using spring-social-salesforce. This makes the assumption that you have set up or are in the process of setting up a connected application in Salesforce. There are a few key pieces of information that will be needed from the connected app definition:
- Consumer Key - Generated when the connected app is created
- Consumer Secret - Generated when the connected app is created
- Callback URL - This will be an endpoint in your java application used as part of the flow discussed further below
- Scopes - The possible values are in parenthesis in the Available OAuth Scopes section of the connected app definition which will be discussed below
- Sandbox (optional) - Are you using a sandbox org? This changes some of the internal authentication logic.
- Instance URL (optional) - This is the URL domain used as the base for requests the Salesforce API. It is not required in all cases. If your application has problems connecting to the REST APIs it may be that you need to have this value set.
Create an instance of the SalesforceConnectionFactory
. Choose the constructor based on your needs. At a minimum, you will need to pass your Consumer Key and Consumer Secret. Each constructor provides more and more configuration. Take into account the information above about Sandbox and Instance URL.
SalesforceConnectionFactory factory = new SalesforceConnectionFactory(Consumer Key, Consumer Secret);
Create an instance of the OAuth2Parameters
object. Here we will need to set at minimum the CallbackURL and a space delimited string of the scope values choosen in the connected app definition.
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.setRedirectUri(CallbackURL);
parameters.setScope(Scopes);
The authentication URL is needed to retrieve the Authentication Code. Once the URL is generated your application should redirect the user to the generated URL. In the example below I am using a GrantType
of AUTHORIZATION_CODE
. This example doesn't cover IMPLICIT_GRANT
.
String authenticationUrl = factory.getOAuthOperations().buildAuthenticateUrl(GrantType.AUTHORIZATION_CODE, parameters);
After the user has accepted the usage of the application Salesforce will redirect the user, through the browser, to the URL set in the Callback URL property of the connected app definition. The URL will have a query parameter of code
. The value of will be exchanged for an AccessGrant
. The grant will have the access and refresh tokens.
AccessGrant accessGrant = factory.getOAuthOperations().exchangeForAccess(accessToken, CallbackURL, null);
The AccessGrant
can be used the create a connection to Salesforce. The connection can then be used to make calls to the Salesforce API as represented in the library.
Connection<Salesforce> connection = factory.createConnection(accessGrant);
connection.getApi()...select the operations object you need...
If you already have access tokens you can use those tokens to create an AccessGrant
object and then create a new connection.
AccessGrant accessGrant = new AccessGrant(AccessToken, Scopes, RefreshToken, null);
Connection<Salesforce> connection = factory.createConnection(accessGrant);
If you used the Perform requests on your behalf at any time (refresh_token, offline_access)
scope you will be able to request a new access token with your refresh token. If you did not then you will need to restart the flow at the generation of the Authentication URL. If using the RefreshToken also fails, most likely because it has also expired, then you will need to restart the flow at the generation of the Authentication URL.
AccessGrant accessGrant = factory.getOAuthOperations().refreshAccess(RefreshToken, null);
Connection<Salesforce> connection = factory.createConnection(accessGrant);