Newer Version Available

This content describes an older version of this product. View Latest

Step 5: Use the Connector with OAuth Bearer Token Login

This step uses the connector with OAuth authentication.

Prerequisites

Obtain an OAuth bearer access token for your Salesforce user. You supply this access token in the connector example.

See Authenticate Apps with OAuth in Salesforce Help and Understanding Authentication in the Force.com REST API Developer Guide.

Let’s run an example that uses OAuth bearer token login.
  1. Under the /src/main/java/ folder, open the BearerTokenExample.java Java source file from the example package folder.
    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;
    16
    17/**
    18 * An example of using the EMP connector using bearer tokens
    19 *
    20 * @author hal.hildebrand
    21 */
    22public class BearerTokenExample {
    23    public static void main(String[] argv) throws Exception {
    24        if (argv.length < 2 || argv.length > 4) {
    25            System.err.println(
    26                "Usage: BearerTokenExample url token topic [replayFrom]");
    27            System.exit(1);
    28        }
    29        long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST;
    30        if (argv.length == 4) {
    31            replayFrom = Long.parseLong(argv[3]);
    32        }
    33
    34        BayeuxParameters params = new BayeuxParameters() {
    35
    36            @Override
    37            public String bearerToken() {
    38                return argv[1];
    39            }
    40
    41            @Override
    42            public URL host() {
    43                try {
    44                    return new URL(argv[0]);
    45                } catch (MalformedURLException e) {
    46                    throw new IllegalArgumentException(
    47                        String.format("Unable to create url: %s", argv[0]), e);
    48                }
    49            }
    50        };
    51
    52        Consumer<Map<String, Object>> consumer = event -> System.out.println(
    53                String.format("Received:\n%s", event));
    54        EmpConnector connector = new EmpConnector(params);
    55
    56        connector.start().get(5, TimeUnit.SECONDS);
    57
    58        TopicSubscription subscription = connector.subscribe(argv[2], 
    59            replayFrom, consumer).get(5, TimeUnit.SECONDS);
    60
    61        System.out.println(String.format("Subscribed: %s", subscription));
    62    }
    63}
  2. Run the BearerTokenExample class, and provide the following argument values.
    Argument Value
    url The URL of the Salesforce instance of the logged-in user.
    token The access token returned by the OAuth authentication flow.
    topic /topic/InvoiceStatementUpdates
    The sample fetches the earliest saved events within the past 24 hours. Optionally, you can supply a replay ID as the last argument to receive different events. 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 last 24 hours.
    • Specific number—Get all events that occurred after the event with the specified replay ID.
  3. 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.}}