接続アプリケーションの定義
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_defining_remote_access_applications.htm
を参考に作成していきます。
ここでは、Web サーバ OAuth 認証フローでやっていきます。
接続アプリケーションのコールバックURLとして利用するWebアプリをGASで作ります。
- スタンドアロンのプロジェクトを新規作成します
- これで作成できます→https://script.google.com/macros/create
- 作成されたプロジェクトはマイドライブに保存されます
- 下部に書いた「コード.gs」を貼り付けて保存します
- Webアプリケーションとして導入します
公開 > ウェブアプリケーションとして導入を開きます
↓
Saleseforceで接続アプリケーションを作っていきます。
1. ホーム画面で「設定」をクリックします。
2. ビルド > 作成 > アプリケーションをクリックします。
3. 「新規」ボタンをクリックします。
4. 基本情報を入力します。
「接続アプリケーション名」「API 参照名」「取引先責任者 メール」「取引先責任者 電話」を入力します。
「OAuth 設定の有効化」にチェックを入れます。
コールバック URLにアプリのURLを入れます。
選択したOAuth 範囲を選択して「追加」ボタンで右のエリアに追加します。
※refresh_tokenを取得するためには、「ユーザに代わっていつでも要求を実行」も追加しておく必要があるようです。
「保存」をクリックします。
真っ白な画面になる場合は上にスクロールして「次へ」をクリックします。
「コンシューマ鍵」がclient_idで「コンシューマの秘密」がclient_secretらしいです。
GASでスクリプトの変数にそれぞれ代入して保存します。
(redirect_uriはWebアプリとして公開したGASのURLを入れます)
スクリプトを変更したのでバージョンを上げて更新します。
↓
6. getMyUrlを実行します。
ログを開きます。
このようなログが出力されています。
7. ログのhttps〜mystateまでをコピーしてブラウザの新規タブで開きます。
このような許可を求められるので「許可」をクリックします。
画面にaccess_tokenやrefresh_tokenなどの情報が表示されます。
スクリプトのプロパティにもそれらの情報が保存されます。
これで接続アプリケーションの作成とアクセストークンの取得ができました。
Webアプリケーションとして導入するGASのスクリプト
コード.gs
var client_id = "";// コンシューマ鍵 var client_secret = ""// コンシューマの秘密 var redirect_uri = "";// このGASプロジェクトをWebアプリとして公開したURL var token_url = 'https://login.salesforce.com/services/oauth2/token'; //認可コード取得URL function getMyUrl() { var url = "https://login.salesforce.com/services/oauth2/authorize?" +// sandboxの場合はinstance_url "response_type=code" + "&" + "client_id=" + client_id + "&" + "redirect_uri=" + redirect_uri + "&" + "state=mystate"; Logger.log(url); } function doGet(e) {// 認可コード取得URLを開いたときに動く処理 var response = getAccessToken(e); setScriptProperties(JSON.parse(response)); return ContentService.createTextOutput(response);// ブラウザに表示する } function getAccessToken(e) {// 認可コードを利用してトークン情報を取得して返す var code = e['parameter']['code']; var payload = { 'grant_type': 'authorization_code', 'client_id': client_id, 'client_secret': client_secret, 'code': code, 'redirect_uri': redirect_uri } var options = { 'method': 'post', 'contentType': 'application/x-www-form-urlencoded', 'payload': payload }; var response = UrlFetchApp.fetch(token_url, options); return response; } function setScriptProperties(jobj) {// スクリプトのプロパティに値を保存する PropertiesService.getScriptProperties().setProperties(jobj); } |
Sandbox環境の場合は以下の通りエンドポイントが異なる
OAuth エンドポイントについて
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_understanding_oauth_endpoints.htm
Sandbox 組織で認証を検証する場合、上記に挙げたすべての OAuth エンドポイントで「login.salesforce.com」の代わりに「test.salesforce.com」を使用してください。
参考
接続アプリケーションの定義
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_defining_remote_access_applications.htm
Lightning Platform REST API の概要
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_what_is_rest_api.htm
Web サーバ OAuth 認証フローでのアプリケーションの認証
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm
接続アプリケーションの定義
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_defining_remote_access_applications.htm
Lightning Platform REST API の概要
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_what_is_rest_api.htm
Web サーバ OAuth 認証フローでのアプリケーションの認証
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm