Last active
January 14, 2019 02:20
-
-
Save na0AaooQ/244b7078781fec420c330985ec068009 to your computer and use it in GitHub Desktop.
cURLコマンドとシェルスクリプトでSalesforce Chatter REST APIを実行してChatterメッセージを投稿する ref: https://qiita.com/na0AaooQ/items/037434830b69e9b45ffd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vi push_salesforce_chatter_message.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sh -n push_salesforce_chatter_message.sh | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ chmod 750 push_salesforce_chatter_message.sh | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./push_salesforce_chatter_message.sh | |
{ | |
"actor" : { | |
"additionalLabel" : null, | |
(中略) | |
}, | |
"body" : { | |
"isRichText" : false, | |
"messageSegments" : [ { | |
"text" : "curlからのテストメッセージ 投稿日時=[20181225_011241] ", | |
"type" : "Text" | |
}, { | |
"accessible" : true, | |
"name" : "** **", | |
"record" : { | |
(中略) | |
"visibility" : "AllUsers" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
export SFDC_HOST="login.salesforce.com" | |
export SFDC_API_VER="v44.0" | |
export SFDC_CLIENT_ID="Salesforceの「設定」->「ビルド」->「作成」->「アプリケーション」のChatter REST API接続用アプリケーションの「コンシューマ鍵」(OAuthコンシューマキー文字列)を記載します。" | |
export SFDC_CLIENT_SECRET="Salesforceの「設定」->「ビルド」->「作成」->「アプリケーション」のChatter REST API接続用アプリケーションの「コンシューマの秘密」(OAuthコンシューマシークレット文字列)を記載します。" | |
export SFDC_CLIENT_USERNAME="Chatter REST APIを有効化しているSalesforceユーザアカウント名を記載します。" | |
export SFDC_CLIENT_AUTHENTICATE_PASSWORD="Chatter REST APIを有効化しているSalesforceユーザアカウントのパスワードを記載します。" | |
## Chatterメッセージ投稿先のSalesforceユーザIDを指定する | |
SFDC_USER_ID="005**********" | |
## ChatterグループID等を指定す | |
## ChatterグループIDはChatterグループURLの以下g=の部分の事( https://ap7.salesforce.com/_ui/core/chatter/groups/GroupProfilePage?g=0F9************ ) | |
SFDC_CHATTER_GROUP_ID="0F9************" | |
## Chatter REST APIのアクセストークンを取得する | |
SFDC_AUTH_RESP=`curl -s https://${SFDC_HOST}/services/oauth2/token -d "grant_type=password" -d "client_id=${SFDC_CLIENT_ID}" -d "client_secret=${SFDC_CLIENT_SECRET}" -d "username=$SFDC_CLIENT_USERNAME" -d "password=${SFDC_CLIENT_AUTHENTICATE_PASSWORD}"`= | |
## 取得したChatter REST APIのアクセストークンをセットする | |
SFDC_ACCESS_TOKEN=`echo "${SFDC_AUTH_RESP}" | awk 'BEGIN{FS="access_token\":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}' | sed -e 's/\"//g'` | |
## 取得したChatter REST APIのSalesforceインスタンスURLをセットする | |
SFDC_INSTANCE_URL=`echo "${SFDC_AUTH_RESP}" | awk 'BEGIN{FS="instance_url\":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}' | sed -e 's/\"//g'` | |
## Chatterメッセージ投稿本文に付加する文字列 | |
SFDC_CHATTER_POST_DATE=`date '+%Y%m%d_%H%m%S'` | |
## Chatter REST APIを呼び出して、Chatterへメッセージを投稿する | |
curl -s -X POST ${SFDC_INSTANCE_URL}/services/data/${SFDC_API_VER}/chatter/feed-elements \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer ${SFDC_ACCESS_TOKEN}" \ | |
-H "X-PrettyPrint:1" \ | |
-d '{ | |
"body" : { | |
"messageSegments" : [ | |
{ | |
"type": "Text", | |
"text": "curlからのテストメッセージ 投稿日時=['${SFDC_CHATTER_POST_DATE}'] " | |
}, | |
{ | |
"type": "Mention", | |
"id": "'${SFDC_USER_ID}'" | |
} | |
] | |
}, | |
"feedElementType": "FeedItem", | |
"subjectId": "'${SFDC_CHATTER_GROUP_ID}'" | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./push_salesforce_chatter_message_badrequest.sh | |
[ { | |
"errorCode" : "MISSING_ARGUMENT", | |
"message" : "Missing required 'subjectId' parameter." | |
} ]$ | |
$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment