Class StateTokenBuilder
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
StateTokenBuilder
อนุญาตให้สคริปต์สร้างโทเค็นสถานะที่ใช้ใน 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;
}
เอกสารประกอบโดยละเอียด
createToken()
สร้างการนําเสนอสตริงที่เข้ารหัสของโทเค็นสถานะ
const stateToken = ScriptApp.newStateToken().createToken();
รีเทิร์น
String
— สตริงที่เข้ารหัสซึ่งแสดงโทเค็น
withArgument(name, value)
เพิ่มอาร์กิวเมนต์ลงในโทเค็น เรียกเมธอดนี้ได้หลายครั้ง
const stateToken =
ScriptApp.newStateToken().withArgument('myField', 'myValue').createToken();
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
name | String | ชื่ออาร์กิวเมนต์ |
value | String | ค่าของอาร์กิวเมนต์ |
รีเทิร์น
StateTokenBuilder
— ตัวสร้างโทเค็นสถานะสําหรับการต่อเชื่อม
withMethod(method)
ตั้งค่าฟังก์ชัน Callback ค่าเริ่มต้นคือฟังก์ชันชื่อ callback()
const stateToken =
ScriptApp.newStateToken().withMethod('myCallback').createToken();
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
method | String | ชื่อของฟังก์ชัน Callback ที่แสดงเป็นสตริงโดยไม่มีวงเล็บหรืออาร์กิวเมนต์ คุณสามารถใช้ฟังก์ชันจากไลบรารีที่รวมไว้ เช่น
Library.libFunction1 |
รีเทิร์น
StateTokenBuilder
— ตัวสร้างโทเค็นสถานะสําหรับการต่อเชื่อม
withTimeout(seconds)
ตั้งค่าระยะเวลา (เป็นวินาที) ที่โทเค็นใช้งานได้ ค่าเริ่มต้นคือ 60 วินาที และระยะเวลาสูงสุดคือ 3,600 วินาที (1 ชั่วโมง)
const stateToken = ScriptApp.newStateToken().withTimeout(60).createToken();
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
seconds | Integer | ระยะเวลาที่โทเค็นใช้งานได้ โดยค่าสูงสุดคือ 3600 |
รีเทิร์น
StateTokenBuilder
— ตัวสร้างโทเค็นสถานะสําหรับการต่อเชื่อม
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe \u003ccode\u003eStateTokenBuilder\u003c/code\u003e allows scripts to generate secure state tokens for use in callback APIs, such as OAuth flows.\u003c/p\u003e\n"],["\u003cp\u003eThese tokens can be customized with arguments, a specific callback function, and a timeout period for enhanced security and functionality.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eScriptApp.newStateToken()\u003c/code\u003e initiates the token creation process, offering methods like \u003ccode\u003ewithArgument()\u003c/code\u003e, \u003ccode\u003ewithMethod()\u003c/code\u003e, and \u003ccode\u003ewithTimeout()\u003c/code\u003e for configuration.\u003c/p\u003e\n"],["\u003cp\u003eFinally, \u003ccode\u003ecreateToken()\u003c/code\u003e generates the encrypted string representation of the state token, ready to be used in your callback URL.\u003c/p\u003e\n"]]],[],null,["# Class StateTokenBuilder\n\nStateTokenBuilder\n\nAllows scripts to create state tokens that can be used in callback APIs (like OAuth flows).\n\n```javascript\n// Reusable function to generate a callback URL, assuming the script has been\n// published as a web app (necessary to obtain the URL programmatically). If the\n// script has not been published as a web app, set `var url` in the first line\n// to the URL of your script project (which cannot be obtained\n// programmatically).\nfunction getCallbackURL(callbackFunction) {\n let url = ScriptApp.getService().getUrl(); // Ends in /exec (for a web app)\n url = `${\n url.slice(0, -4)}usercallback?state=`; // Change /exec to /usercallback\n const stateToken = ScriptApp.newStateToken()\n .withMethod(callbackFunction)\n .withTimeout(120)\n .createToken();\n return url + stateToken;\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------------|------------------------|-------------------------------------------------------------------|\n| [createToken()](#createToken()) | `String` | Constructs an encrypted string representation of the state token. |\n| [withArgument(name, value)](#withArgument(String,String)) | [StateTokenBuilder](#) | Adds an argument to the token. |\n| [withMethod(method)](#withMethod(String)) | [StateTokenBuilder](#) | Sets a callback function. |\n| [withTimeout(seconds)](#withTimeout(Integer)) | [StateTokenBuilder](#) | Sets the duration (in seconds) for which the token is valid. |\n\nDetailed documentation\n----------------------\n\n### `create``Token()`\n\nConstructs an encrypted string representation of the state token.\n\n```javascript\nconst stateToken = ScriptApp.newStateToken().createToken();\n```\n\n#### Return\n\n\n`String` --- an encrypted string representing the token\n\n*** ** * ** ***\n\n### `with``Argument(name, value)`\n\nAdds an argument to the token. This method can be called multiple times.\n\n```javascript\nconst stateToken =\n ScriptApp.newStateToken().withArgument('myField', 'myValue').createToken();\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|----------|---------------------------|\n| `name` | `String` | the name of the argument |\n| `value` | `String` | the value of the argument |\n\n#### Return\n\n\n[StateTokenBuilder](#) --- the state token builder, for chaining\n\n*** ** * ** ***\n\n### `with``Method(method)`\n\nSets a callback function. The default is a function named `callback()`.\n\n```javascript\nconst stateToken =\n ScriptApp.newStateToken().withMethod('myCallback').createToken();\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `method` | `String` | The name of the callback function, represented as a string without parentheses or arguments. You can use functions from included libraries, such as ` Library.libFunction1`. |\n\n#### Return\n\n\n[StateTokenBuilder](#) --- the state token builder, for chaining\n\n*** ** * ** ***\n\n### `with``Timeout(seconds)`\n\nSets the duration (in seconds) for which the token is valid. The defaults is 60 seconds; the\nmaximum duration is 3600 seconds (1 hour).\n\n```javascript\nconst stateToken = ScriptApp.newStateToken().withTimeout(60).createToken();\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------|-----------|------------------------------------------------------------------------|\n| `seconds` | `Integer` | the duration for which the token is valid; the maximum value is `3600` |\n\n#### Return\n\n\n[StateTokenBuilder](#) --- the state token builder, for chaining"]]