Salesforce extension for Spring Social

For the past two weeks I have been working on Spring Social Salesforce. Spring Social Salesforce is an extension to Spring Social that provides Salesforce connect and api bindings support.

Spring Social

Spring Social is an extension of the Spring Framework that allows you to connect your applications with Software-as-a-Service (SaaS) providers such as Facebook and Twitter.

As stated on it's site; Spring Social is an abstraction on top of social platforms (API's) that takes the burden of connecting your applications via OAuth and provides a clean way to implement rest clients.

I am sure there are different libraries that enables you to connect your applications to Salesforce, e.g. Salesforce SOAP client, etc. However Spring Social provides some unique features that you may want to reconsider your choices.

  • It handles the OAuth negotiation out-of-the-box and in an elegant way.
  • Provides connection management and connection persistence so you don't have to worry about the session management.
  • As usual, it works very well with Spring Framework.
  • With the help of Spring RestTemplate and Jackson it is very easy to code against any rest API.

See the following one-liner that fetches a user's profile.

@Override
public SalesforceProfile getUserProfile(String userId) {
    requireAuthorization();
    return restTemplate.getForObject(api.getBaseUrl() + "/v23.0/chatter/users/{id}",
            SalesforceProfile.class, userId);
}

Spring Social Salesforce

Enough about Spring Social, let's see what you can do with Spring Social Salesforce. Spring Social Salesforce is still at an early stage so the force.com REST API is not fully supported, there are still lots of things that are missing, especially the ones related to the Chatter API. I will be adding new features and complete the API in the coming months. Luckily we use the very same extension to integrate our applications with Salesforce in my current company so it will be easier for me to keep my promise :)

See the following for the list of API's supported and their coverage.

  • Api operations: Fully implemented.
  • sObject Operations: All read-only operations are fully implemented.
  • Query Opreations: Fully implemented.
  • Search Operations: Fully implemented.
  • Recent Opreations: Fully implemented
  • Chatter Operations: Only user profile retrieval, status retrival and update are implemented. This is the least covered API but new features will be gradually added.

Token Refresh

One big caveat of the current implementation that it does not handle token refresh transparently. One has to handle it manually via wrapping every call with a try-catch block.

try {
    SalesforceProfile profile = salesforce.chatterOperations().getUserProfile();
} catch (InvalidAuthorizationException e) {
    //call refresh on connection and repeat the same call
}

I agree, it is annoying. Luckily there is a reported feature request and it will be implemented by spring social team however it is not yet scheduled. See SOCIAL-263.

There are some ninja tricks that can be done to handle it transparently however I did not include my workaround in the project source code. If you are interested in, just let me know, happy to help.

Client Login

Salesforce API also supports authenticating via client login, with username/password. For those who don't need OAuth negotiation can leverage this approach. Your application may not be a multi-tenant application and all you need is to use integrate a backend system with Salesforce. In this case you may not have the opportunity or the place to do the OAuth dance (redirect, etc) and you may also want the whole flow to be automatic.

With the client login, you make a POST to Salesforce token service with your username/password to get your access token. And good news is that this token never expires!

In the source code you will see a package named org.springframework.social.salesforce.client that contains a factory and utility classes to use the extension without OAuth mechanism.

See the following code for the usage. It calls factory's create method with necessary credentials and gets a Salesforce template configured with the retrieved access token.

SalesforceFactory factory = new BaseSalesforceFactory(clientid, clientSecret);
Salesforce template = factory.create(username, password, secretToken);

References

blog comments powered by Disqus