Class AuthorizationInfo

授权信息

用于检查用户是否已授予脚本所需范围的授权的对象。该对象还提供了一个授权网址,供用户授予这些权限。

某些脚本执行可以在用户未同意脚本使用的所有必需范围的情况下开始。通过此对象中的信息,您可以控制对需要特定范围的代码部分的访问权限,并请求对这些范围进行授权以供后续执行。

此对象由 ScriptApp.getAuthorizationInfo(authMode) 返回。在几乎所有情况下,脚本都应调用 ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL),因为没有其他授权模式需要用户授予授权。

方法

方法返回类型简介
getAuthorizationStatus()AuthorizationStatus获取一个值,该值指示用户是否需要授权此脚本使用一项或多项服务(例如 ScriptApp.AuthorizationStatus.REQUIRED)。
getAuthorizationUrl()String|null获取可用于授予脚本访问权限的授权网址。
getAuthorizedScopes()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 - 授权范围的列表。