Skip to content

Latest commit

 

History

History
139 lines (103 loc) · 5.53 KB

README.adoc

File metadata and controls

139 lines (103 loc) · 5.53 KB

Twitter Salesforce Example

Introduction

This example listen for tweets which are mentioning the @cameltweet twitter account. Information from the user whom mentions is then created as a contact in Salesforce.

Configuring Twitter

This example ships with the keys and tokens of a long-standing testing Twitter/X account named cameltweet, used only to let the example show a complete, working configuration; they are not a working credential you can rely on. They predate Twitter/X’s 2023 API overhaul and are very likely no longer valid, and even if they were, they are for a shared testing account, not something to build on.

You must self-provision your own Twitter/X developer app and use its consumer key/secret and access token/secret in place of the values in application.properties before this example will actually run.

Configuring Salesforce

This example ships with clientId/clientSecret values for a Camel test Salesforce developer account, included only to show what a filled-in configuration looks like. As with the Twitter/X keys above, self-provision your own Salesforce Developer account at https://developer.salesforce.com/ for anything beyond reading this example, and follow the steps below to create your own Connected Application for your integration.

To do this after logging in to your Salesforce Developer account, navigate to Apps located under Build and then Create, there you should see Connected Apps table in the heading click on New and fill in the indicated required fields and enable the OAuth Settings, for Callback URL you can use https://login.salesforce.com/services/oauth2/success.

In the Available OAuth Scopes add Access and manage your data (api) and Perform requests on your behalf at any time (refresh_token, offline_access).

After clicking Save click on Manage on the top of the page and then click on Edit Policies. Change the IP Relaxation to Relax IP restrictions and click on Save.

NOTE: This will get you started quicker, but production you should re-evaluate to comply with your security needs.

Next gather your Consumer Key (clientId property), Consumer Secret (clientSecret) and either use username and password of the developer account; or get the refresh token from Salesforce (more on this below).

Adding the Twitter screen name custom field

The example adds a custom field to Contact SObject, to add it to your Salesforce environment go into Customize under Build and choose Fields under Contact.

In Contact Custom Fields & Relationships click on New and add a field of type Text with field label Twitter Screen Name, length of 15 and for uniqueness select Do not allow duplicate values and set the Set this field as the unique record identifier from an external system.

Getting the OAuth refresh token

In your browser go to the URL change the __YOUR_CLIENT_ID_HERE__ with your connected application Consumer Key:

https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=YOUR_CLIENT_ID_HERE&redirect_uri=https://login.salesforce.com/services/oauth2/success&display=touch

Allow access to the application, and you’ll end up on a page with refresh_token after the #, something like:

https://login.salesforce.com/services/oauth2/success#access_token=..&refresh_token=<refresh_token>&instance_url=...&issued_at=...&signature=...&scope=...&token_type=Bearer

How to generate Salesforce Data Transfer Objects (DTOs)

The best way to generate Java representation of Salesforce SObjects is to use the camel-salesforce-maven-plugin and specify the desired DTO in <configuration> section, for example:

        <configuration>
            <clientId>${camelSalesforce.clientId}</clientId>
            <clientSecret>${camelSalesforce.clientSecret}</clientSecret>
            <userName>${camelSalesforce.userName}</userName>
            <password>${camelSalesforce.password}</password>
            <includes>
                <include>Contact</include>
            </includes>
        </configuration>

To generate only DTOs needed for Contact, but the parameter value can be specified using regular expressions.

Running that plugin needs a live login against a real org, so it only runs under the generate-salesforce-dto profile (-Dgenerate.dto), never during a plain mvn compile. This example itself only sets LastName and TwitterScreenName__c, so it doesn’t need a generated DTO - it uses the minimal, hand-maintained org.apache.camel.example.mention.dto.Contact bundled with the example, which is enough to compile and run without any Salesforce credentials at build time. The plugin and profile are kept around as a starting point if you want to extend the route to read or write additional fields on Contact (or other SObjects) - generate a DTO for them and use it in place of the bundled one.

Build

You will need to compile this example first:

mvn compile

Run

To run the example type using generate.dto profile

mvn -DcamelSalesforce.clientId=3<client id> \
    -DcamelSalesforce.clientSecret=<client secret> \
    -DcamelSalesforce.userName=<username> \
    -DcamelSalesforce.password=<password> -Dgenerate.dto spring-boot:run

To stop the example hit ctrl+c

Help and contributions

If you hit any problem using Camel or have some feedback, then please let us know.

We also love contributors, so get involved :-)

The Camel riders!