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()
라는 함수입니다.
const stateToken =
ScriptApp.newStateToken().withMethod('myCallback').createToken();
매개변수
이름 | 유형 | 설명 |
method | String | 콜백 함수의 이름으로, 괄호나 인수 없이 문자열로 표현됩니다. 포함된 라이브러리(예:
Library.libFunction1 )의 함수를 사용할 수 있습니다. |
리턴
StateTokenBuilder
: 체이닝을 위한 상태 토큰 빌더
withTimeout(seconds)
토큰이 유효한 기간 (초)을 설정합니다. 기본값은 60초이며 최대 시간은 3,600초 (1시간)입니다.
const stateToken = ScriptApp.newStateToken().withTimeout(60).createToken();
매개변수
이름 | 유형 | 설명 |
seconds | Integer | 토큰이 유효한 기간입니다. 최대값은 3600 입니다. |
리턴
StateTokenBuilder
: 체이닝을 위한 상태 토큰 빌더
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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"]]