認証フローへの OAuth 更新トークンの適合
にかかれていることをGASでやってみます。
事前準備
接続アプリケーションを作成して
アクセストークンなどのデータをスクリプトのプロパティに保存しておく
手順は以下のブログに書きました。
SalesforceでAPIを使うために接続アプリケーションを作成したい
Salesforceで接続アプリケーションを作成したい(Lightningの方で)
コード.gs
function runRefresh() {// refresh_tokenを使って更新したトークン情報を返す var payload = { 'grant_type': 'refresh_token', 'refresh_token': getProp("refresh_token"), 'client_id': client_id, 'client_secret': client_secret } var options = { 'method': 'post', 'contentType': 'application/x-www-form-urlencoded', 'payload': payload } var response = UrlFetchApp.fetch("https://login.salesforce.com/services/oauth2/token", options); Logger.log(response); setScriptProperties(JSON.parse(response)); } function getProp(key) { return PropertiesService.getScriptProperties().getProperty(key); } function setScriptProperties(jobj) {// スクリプトのプロパティに値を保存する PropertiesService.getScriptProperties().setProperties(jobj); } |
関連記事
SalesforceでAPIを使うために接続アプリケーションを作成したい
Salesforceで接続アプリケーションを作成したい(Lightningの方で)
参考
OAuth 2.0 Refresh Token Flow for Renewed Sessions
https://help.salesforce.com/articleView?id=remoteaccess_oauth_refresh_token_flow.htm&type=5
Web サーバ OAuth 認証フローでのアプリケーションの認証
https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm
認証フローへの OAuth 更新トークンの適合
https://developer.salesforce.com/docs/atlas.ja-jp.224.0.api_rest.meta/api_rest/intro_understanding_refresh_token_oauth.htm