この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

ステップ 5: OAuth べアラートークンログインを使用してコネクタを使用する

このステップでは、OAuth 認証を使用したコネクタを使用します。

前提条件

Salesforce ユーザの OAuth ベアラーアクセストークンを入手します。このアクセストークンをコネクタの例で使用します。

Salesforce ヘルプ「OAuth によるアプリケーションの認証」および『Force.com REST API 開発者ガイド』「認証について」を参照してください。

OAuth ベアラートークンログインを使用する例を実行しましょう。
  1. /src/main/java/ フォルダで、例のパッケージフォルダから BearerTokenExample.java Java ソースファイルを開きます。
    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 * @since 202
    22 */
    23public class BearerTokenExample {
    24    public static void main(String[] argv) throws Exception {
    25        if (argv.length < 2 || argv.length > 4) {
    26            System.err.println(
    27                "Usage: BearerTokenExample url token topic [replayFrom]");
    28            System.exit(1);
    29        }
    30        long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST;
    31        if (argv.length == 4) {
    32            replayFrom = Long.parseLong(argv[3]);
    33        }
    34
    35        BayeuxParameters params = new BayeuxParameters() {
    36
    37            @Override
    38            public String bearerToken() {
    39                return argv[1];
    40            }
    41
    42            @Override
    43            public URL host() {
    44                try {
    45                    return new URL(argv[0]);
    46                } catch (MalformedURLException e) {
    47                    throw new IllegalArgumentException(
    48                        String.format("Unable to create url: %s", argv[0]), e);
    49                }
    50            }
    51        };
    52
    53        Consumer<Map<String, Object>> consumer = event -> System.out.println(
    54                String.format("Received:\n%s", event));
    55        EmpConnector connector = new EmpConnector(params);
    56
    57        connector.start().get(5, TimeUnit.SECONDS);
    58
    59        TopicSubscription subscription = connector.subscribe(argv[2], 
    60            replayFrom, consumer).get(5, TimeUnit.SECONDS);
    61
    62        System.out.println(String.format("Subscribed: %s", subscription));
    63    }
    64}
  2. BearerTokenExample クラスを実行し、次の引数の値を指定します。
    引数
    url ログインユーザの Salesforce インスタンスの URL。
    トークン OAuth 認証フローによって返されたアクセストークン。
    topic /topic/InvoiceStatementUpdates
    このサンプルでは、過去 24 時間以内で最も早く保存されたイベントを取得します。必要に応じて、最後の引数として再生 ID を指定して異なるイベントを取得できます。有効な値は、次のとおりです。
    • -1 — 登録後に送信されたすべての新規イベントを取得します。
    • -2 — 登録後に送信されたすべての新規イベントおよび過去 24 時間以内のすべての過去のイベントを取得します。
    • 特定の番号 - 指定された再生 ID を持つイベントより後に発生したすべてのイベントを取得します。
  3. ブラウザウィンドウで、請求書明細を作成または変更します。PushTopic 内のクエリに対応するデータを作成または変更すると、出力は次のようになります。
    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.}}