Class StateTokenBuilder Stay organized with collections Save and categorize content based on your preferences.
Page Summary
StateTokenBuilder allows scripts to create state tokens for use in callback APIs like OAuth flows.
The
getCallbackURLfunction demonstrates how to generate a callback URL programmatically using a state token.Methods available include
createToken(),withArgument(),withMethod(), andwithTimeout()to configure the state token.createToken()generates the encrypted string representation of the token.withTimeout()sets the token's validity duration in seconds, with a maximum of 3600 seconds.
Allows scripts to create state tokens that can be used in callback APIs (like OAuth flows).
// 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).functiongetCallbackURL(callbackFunction){leturl=ScriptApp.getService().getUrl();// Ends in /exec (for a web app)url=`${url.slice(0,-4)}usercallback?state=`;// Change /exec to /usercallbackconststateToken=ScriptApp.newStateToken().withMethod(callbackFunction).withTimeout(120).createToken();returnurl+stateToken;}
Methods
| Method | Return type | Brief description |
|---|---|---|
create | String | Constructs an encrypted string representation of the state token. |
with | State | Adds an argument to the token. |
with | State | Sets a callback function. |
with | State | Sets the duration (in seconds) for which the token is valid. |
Detailed documentation
createToken()
Constructs an encrypted string representation of the state token.
conststateToken=ScriptApp.newStateToken().createToken();
Return
String — an encrypted string representing the token
withArgument(name, value)
Adds an argument to the token. This method can be called multiple times.
conststateToken=ScriptApp.newStateToken().withArgument('myField','myValue').createToken();
Parameters
| Name | Type | Description |
|---|---|---|
name | String | the name of the argument |
value | String | the value of the argument |
Return
State — the state token builder, for chaining
withMethod(method)
Sets a callback function. The default is a function namedcallback().
conststateToken=ScriptApp.newStateToken().withMethod('myCallback').createToken();
Parameters
| Name | Type | Description |
|---|---|---|
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. |
Return
State — the state token builder, for chaining
withTimeout(seconds)
Sets the duration (in seconds) for which the token is valid. The defaults is 60 seconds; themaximum duration is 3600 seconds (1 hour).
conststateToken=ScriptApp.newStateToken().withTimeout(60).createToken();
Parameters
| Name | Type | Description |
|---|---|---|
seconds | Integer | the duration for which the token is valid; the maximum value is3600 |
Return
State — the state token builder, for chaining
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.