此对象用于检查用户是否已为脚本的必需范围授予授权。此对象还提供了一个授权网址,供用户授予这些权限。
某些脚本执行操作可以在用户未同意脚本使用的所有必需范围的情况下启动。借助此对象中的信息,您可以控制对需要特定范围的代码部分的访问权限,并请求对这些范围进行授权以供后续执行。
此对象由 ScriptApp.getAuthorizationInfo(authMode) 返回。在几乎所有情况下,脚本都应调用
ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL),因为没有其他授权模式要求用户授予授权。
方法
| 方法 | 返回值类型 | 简介 |
|---|---|---|
get | Authorization | 获取一个值,该值指示用户是否需要授权此脚本使用一项或多项
服务(例如 Script)。 |
get | String|null | 获取可用于授予对脚本的访问权限的授权网址。 |
get | String[]|null | 获取脚本的授权范围列表。 |
详细文档
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|null - 可用于授权脚本的网址。
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[]|null - 授权范围列表。