LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Friday, January 31, 2020

Salesforce APIでrefresh tokenを使いたい


認証フローへの 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の方で)


参考

Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...