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:授權範圍清單。