Newer Version Available
(Optional) Step 5: Use the Connector with OAuth Bearer Token Login
You can use the connector with OAuth authentication as an alternative to username and
password authentication. This step is optional and requires an OAuth token.
Prerequisites
Obtain an OAuth bearer access token for your Salesforce user. You supply this access token in the connector example.See Set Up Authentication with OAuth 2.0. Also see Authenticate Apps with OAuth in Salesforce Help and Understanding Authentication in the REST API Developer Guide.
-
In the
/src/main/java/com/salesforce/emp/connector/example
folder, open the BearerTokenExample.java Java source
file.
1/* 2 * Copyright (c) 2016, salesforce.com, inc. All rights reserved. Licensed under the BSD 3-Clause license. For full 3 * license text, see LICENSE.TXT file in the repo root or https://opensource.org/licenses/BSD-3-Clause 4 */ 5package com.salesforce.emp.connector.example; 6 7import java.net.MalformedURLException; 8import java.net.URL; 9import java.util.Map; 10import java.util.concurrent.TimeUnit; 11import java.util.function.Consumer; 12 13import com.salesforce.emp.connector.BayeuxParameters; 14import com.salesforce.emp.connector.EmpConnector; 15import com.salesforce.emp.connector.TopicSubscription; 16import org.cometd.bayeux.Channel; 17 18/** 19 * An example of using the EMP connector using bearer tokens 20 */ 21public class BearerTokenExample { 22 public static void main(String[] argv) throws Exception { 23 if (argv.length < 2 || argv.length > 4) { 24 System.err.println("Usage: BearerTokenExample url token channel [replayFrom]"); 25 System.exit(1); 26 } 27 long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST; 28 if (argv.length == 4) { 29 replayFrom = Long.parseLong(argv[3]); 30 } 31 32 BayeuxParameters params = new BayeuxParameters() { 33 34 @Override 35 public String bearerToken() { 36 return argv[1]; 37 } 38 39 @Override 40 public URL host() { 41 try { 42 return new URL(argv[0]); 43 } catch (MalformedURLException e) { 44 throw new IllegalArgumentException(String.format( 45 "Unable to create url: %s", argv[0]), e); 46 } 47 } 48 }; 49 50 Consumer<Map<String, Object>> consumer = event -> System.out.println( 51 String.format("Received:\n%s", event)); 52 EmpConnector connector = new EmpConnector(params); 53 54 connector.addListener(Channel.META_CONNECT, new LoggingListener(true, true)) 55 .addListener(Channel.META_DISCONNECT, new LoggingListener(true, true)) 56 .addListener(Channel.META_HANDSHAKE, new LoggingListener(true, true)); 57 58 connector.start().get(5, TimeUnit.SECONDS); 59 60 TopicSubscription subscription = connector.subscribe( 61 argv[2], replayFrom, consumer).get(5, TimeUnit.SECONDS); 62 63 System.out.println(String.format("Subscribed: %s", subscription)); 64 } 65} -
Run the BearerTokenExample class, and provide the following argument
values.
Argument Value url URL of the Salesforce instance of the logged-in user token The access token returned by the OAuth authentication flow channel The channel name for the PushTopic: /topic/InvoiceStatementUpdates The sample fetches the earliest saved events within the past 24 hours. Optionally, to receive different events, you can include a replay ID as the last argument. Valid values are:- –1—Get all new events sent after subscription.
- –2—Get all new events sent after subscription and all past events within the past 24 hours.
- Specific number—Get all events that occurred after the event with the specified replay ID.
-
In a browser window, create or modify an invoice statement. After you create or
change data that corresponds to the query in your PushTopic, the output looks
similar to the following.
1Subscribed: Subscription [/topic/InvoiceStatementUpdates:-2] 2Received: 3{event={createdDate=2016-12-12T22:31:48.035Z, replayId=1, type=created}, sobject={Status__c=Open, Id=a070P00000pn0hyQAA, Name=INV-0001, Description__c=blah}} 4Received: 5{event={createdDate=2016-12-12T22:32:06.440Z, replayId=2, type=updated}, sobject={Status__c=Negotiating, Id=a070P00000pn0hyQAA, Name=INV-0001, Description__c=blah}} 6Received: 7{event={createdDate=2016-12-12T22:32:57.404Z, replayId=3, type=created}, sobject={Status__c=Open, Id=a070P00000pn0lfQAA, Name=INV-0002, Description__c=Laptops and accessories.}}