允許指令碼建立可在回呼 API (例如 OAuth 流程) 中使用的狀態權杖。
// Reusable function to generate a callback URL, assuming the script has been // published as a web app (necessary to obtain the URL programmatically). If the // script has not been published as a web app, set `var url` in the first line // to the URL of your script project (which cannot be obtained // programmatically). function getCallbackURL(callbackFunction) { let url = ScriptApp.getService().getUrl(); // Ends in /exec (for a web app) url = `${ url.slice(0, -4)}usercallback?state=`; // Change /exec to /usercallback const stateToken = ScriptApp.newStateToken() .withMethod(callbackFunction) .withTimeout(120) .createToken(); return url + stateToken; }
方法
| 方法 | 傳回類型 | 簡短說明 | 
|---|---|---|
| create | String | 建構狀態符記的加密字串表示法。 | 
| with | State | 將引數新增至符記。 | 
| with | State | 設定回呼函式。 | 
| with | State | 設定權杖的有效時間 (以秒為單位)。 | 
內容詳盡的說明文件
create
建構狀態符記的加密字串表示法。
const stateToken = ScriptApp.newStateToken().createToken();
回攻員
String:代表權杖的加密字串
with
將引數新增至符記。這個方法可以多次呼叫。
const stateToken = ScriptApp.newStateToken().withArgument('myField', 'myValue').createToken();
參數
| 名稱 | 類型 | 說明 | 
|---|---|---|
| name | String | 引數的名稱 | 
| value | String | 引數的值 | 
回攻員
State:狀態權杖建構工具,用於鏈結
with
設定回呼函式。預設為名為 callback() 的函式。
const stateToken = ScriptApp.newStateToken().withMethod('myCallback').createToken();
參數
| 名稱 | 類型 | 說明 | 
|---|---|---|
| method | String | 回呼函式的名稱,以不含括號或引數的字串表示。您可以使用所納入程式庫的函式,例如 
    Library.libFunction1。 | 
回攻員
State:狀態權杖建構工具,用於鏈結
with
設定權杖的有效時間 (以秒為單位)。預設值為 60 秒,最長時間為 3600 秒 (1 小時)。
const stateToken = ScriptApp.newStateToken().withTimeout(60).createToken();
參數
| 名稱 | 類型 | 說明 | 
|---|---|---|
| seconds | Integer | 權杖有效期間,最大值為 3600 | 
回攻員
State:狀態權杖建構工具,用於鏈結