Class AuthorizationInfo
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
授权信息
用于检查用户是否已授予脚本所需作用域的授权的对象。该对象还会提供授权网址,供用户授予这些权限。
在用户未同意脚本使用的所有必需权限的情况下,某些脚本执行可能会开始。借助此对象中的信息,您可以控制对需要特定作用域的代码段的访问权限,并请求授予这些作用域的授权以进行后续执行。
此对象由 ScriptApp.getAuthorizationInfo(authMode)
返回。在几乎所有情况下,脚本都应调用 ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)
,因为没有其他授权模式需要用户授予授权。
详细文档
getAuthorizationStatus()
获取一个值,该值表示用户是否需要授权此脚本使用一个或多个服务(例如 ScriptApp.AuthorizationStatus.REQUIRED
)。
// Log the authorization status (REQUIRED or NOT_REQUIRED).
const authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
Logger.log(authInfo.getAuthorizationStatus());
返回
AuthorizationStatus
- 授权状态
getAuthorizationUrl()
获取可用于授予对脚本的访问权限的授权网址。如果无需授权,此方法会返回 null
。如果网址被访问且脚本不需要任何授权,该网址对应的页面将自动关闭。
// Log the URL used to grant access to the script.
const authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
Logger.log(authInfo.getAuthorizationUrl());
返回
String
- 可用于授权脚本的网址
getAuthorizedScopes()
获取脚本的已获授权范围列表。如果系统针对指定的范围列表请求授权信息,则返回指定列表中的已授权范围。
// Logs which scopes in the specified list have been authorized for the script.
const authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL, [
'https: //www.googleapis.com/auth/documents',
'https: //www.googleapis.com/auth/spreadsheets',
]);
Logger.log(authInfo.getAuthorizedScopes());
返回
String[]
- 已获授权的范围列表。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eAuthorizationInfo is an object used to determine if a user needs to authorize a script to use services and provides the authorization dialog URL.\u003c/p\u003e\n"],["\u003cp\u003eThis object is primarily used in add-ons with installable triggers to manage user access or prompt for authorization.\u003c/p\u003e\n"],["\u003cp\u003eIt offers two methods: \u003ccode\u003egetAuthorizationStatus()\u003c/code\u003e to check if authorization is required, and \u003ccode\u003egetAuthorizationUrl()\u003c/code\u003e to obtain the authorization URL.\u003c/p\u003e\n"],["\u003cp\u003eScripts should generally use \u003ccode\u003eScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL)\u003c/code\u003e for comprehensive authorization checks.\u003c/p\u003e\n"]]],[],null,["# Class AuthorizationInfo\n\nAuthorizationInfo\n\nAn object that checks if the user has granted authorization for the required scopes of the\nscript. The object also provides an authorization URL for users to grant those permissions.\n\nSome script executions can start without a user's consent to all required scopes used by the\nscript. The information in this object lets you control access to sections of code that require\ncertain scopes and request authorization of those scopes for subsequent executions.\n\nThis object is returned by [ScriptApp.getAuthorizationInfo(authMode)](/apps-script/reference/script/script-app#getAuthorizationInfo(AuthMode)). In almost\nall cases, scripts should call `Script``App.getAuthorizationInfo(ScriptApp.AuthMode.FULL)`,\nsince no other authorization mode requires that users grant authorization. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------|---------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [getAuthorizationStatus()](#getAuthorizationStatus()) | [AuthorizationStatus](/apps-script/reference/script/authorization-status) | Gets a value that indicates whether the user needs to authorize this script to use one or more services (for example, `Script``App.AuthorizationStatus.REQUIRED`). |\n| [getAuthorizationUrl()](#getAuthorizationUrl()) | `String` | Gets the authorization URL that can be used to grant access to the script. |\n| [getAuthorizedScopes()](#getAuthorizedScopes()) | `String[]` | Gets a list of authorized scopes for the script. |\n\nDetailed documentation\n----------------------\n\n### `get``Authorization``Status()`\n\nGets a value that indicates whether the user needs to authorize this script to use one or more\nservices (for example, `Script``App.AuthorizationStatus.REQUIRED`).\n\n```javascript\n// Log the authorization status (REQUIRED or NOT_REQUIRED).\nconst authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);\nLogger.log(authInfo.getAuthorizationStatus());\n```\n\n#### Return\n\n\n[AuthorizationStatus](/apps-script/reference/script/authorization-status) --- the authorization status\n\n*** ** * ** ***\n\n### `get``Authorization``Url()`\n\nGets the authorization URL that can be used to grant access to the script. This method returns\n`null` if no authorization is required. The page at the URL will close automatically if\nit is accessed and the script does not require any authorization.\n\n```javascript\n// Log the URL used to grant access to the script.\nconst authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);\nLogger.log(authInfo.getAuthorizationUrl());\n```\n\n#### Return\n\n\n`String` --- a URL that can be used to authorize the script\n\n*** ** * ** ***\n\n### `get``Authorized``Scopes()`\n\nGets a list of authorized scopes for the script. If authorization information is requested for\na specified list of scopes, returns the authorized scopes from the specified list.\n\n```javascript\n// Logs which scopes in the specified list have been authorized for the script.\nconst authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL, [\n 'https: //www.googleapis.com/auth/documents',\n 'https: //www.googleapis.com/auth/spreadsheets',\n]);\nLogger.log(authInfo.getAuthorizedScopes());\n```\n\n#### Return\n\n\n`String[]` --- The list of authorized scopes."]]