railsからSalesforce Chatterフィードを取得


やること

  • Salesforceに「接続アプリケーション」作成
  • rails からChatter REST APIを叩き、Chatter Feed取得

実装

Salesforce 接続アプリケーション作成

SalesforceにRESTでアクセスするための設定を行う
(https://web.plus-idea.net/2016/06/salesforce-rest-api-call-setting/)
を参考に

「コンシューマ鍵」と「コンシューマの秘密」の2つをApiコール時に使用

rails からChatter REST APIを叩き、Chatter Feed取得

Controller.rb

require 'net/http'
require 'uri'
require 'json'
require 'net/https'

class HelloController < ApplicationController
  def index

    # OAuth 2.0認証よりアクセストークン取得
    uri = URI.parse("https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=<コンシューマ鍵>&client_secret=<コンシューマの秘密>&username=<Salesforceユーザ>&password=<パスワード>")

    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true
    https.set_debug_output $stderr
    req = Net::HTTP::Post.new(uri.request_uri)
    req["Content-Type"] = "application/x-www-form-urlencoded"

    res = https.request(req)
    @result = JSON.parse(res.body)
    # アクセストークン取得
    @token = @result["access_token"]

    # GETリクエスト→Chatter Feed取得
    url = URI.parse('https://login.salesforce.com/services/data/v43.0/chatter/feeds/news/me/feed-elements')

    req = Net::HTTP::Get.new(url.request_uri)
    req["Authorization"] = 'Bearer ' << @token

    # フィード情報取得
    res = https.request(req)

    @feedsData = res.body

  end
end

※注意点
1. Salesforceの設定で、「私のドメイン」を設定していると、
https://login.salesforce.com/ → https://<私のドメイン>.salesforce.com/
となる。

<フィード取得結果サンプル(JSON)>

{
    "currentPageToken": null,
    "currentPageUrl": "/services/data/v43.0/chatter/feeds/news/00528000003oTdCAAU/feed-elements",
    "elements": [
        {
            "actor": {
                "additionalLabel": null,
                "communityNickname": "iseki1",
                "companyName": "companyName",
                "displayName": "displayName",
                "firstName": "displayName",
                "id": "00528000003oTdCAAU",
                "isActive": true,
                "isInThisCommunity": true,
                "lastName": "displayName",
                "motif": {
                    "color": "65CAE4",
                    "largeIconUrl": "/img/icon/profile64.png",
                    "mediumIconUrl": "/img/icon/profile32.png",
                    "smallIconUrl": "/img/icon/profile16.png",
                    "svgIconUrl": null
                },
                "mySubscription": null,
                "name": "displayName",
                "outOfOffice": {
                    "message": ""
                },
                "photo": {
                    "fullEmailPhotoUrl": "",
                    "largePhotoUrl": "",
                    "mediumPhotoUrl": "",
                    "photoVersionId": null,
                    "smallPhotoUrl": "",
                    "standardEmailPhotoUrl": "",
                    "url": "/services/data/v43.0/connect/user-profiles/00528000003oTdCAAU/photo"
                },
                "reputation": null,
                "stamps": [],
                "title": null,
                "type": "User",
                "url": "/services/data/v43.0/chatter/users/00528000003oTdCAAU",
                "userType": "Internal"
            },
            "body": {
                "isRichText": true,
                "messageSegments": [
                    {
                        "htmlTag": "p",
                        "markupType": "Paragraph",
                        "text": "",
                        "type": "MarkupBegin"
                    },
                    {
                        "text": "ここにフィードの内容が記載される ",
                        "type": "Text"
                    },
                    {
                        "htmlTag": "p",
                        "markupType": "Paragraph",
                        "text": "\n",
                        "type": "MarkupEnd"
                    }
                ],
                "text": "ここにフィードの内容が記載される "
            },

環境

Rails 5.2.0
Salesforce Developer Edition(Summer 18)