Google Drive: Delete Shared Link

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_User
C1: User connects to Google Drive (must be App Administrator) *
conf_FileId
C2: File / Folder ID from which to delete the shared link *

Notes

  • Users specified in C1 need to have a configured connection with Google Drive in [Account Settings] > [Google Connectivity]
    • Google Workspace Connectivity ([System Settings] > [Google Connectivity]) must be enabled on the workflow platform ([System Administrator Authorization] required )
  • You can delete shared links of files and folders created by [Google Drive: File Upload], [Google Drive: Create Folder], and [Google Sheets: Create File].

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • google-drive-permission-delete.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step


function main() {
    const quser = getQuser();
    const fileId = getFileId();
    const permissionId = getPermissionIdToDelete(quser, fileId);
    deletePermission(quser, fileId, permissionId);
}

/**
 * 実行ユーザを取得する
 * @return {QuserView} 実行ユーザ
 */
function getQuser() {
    const quser = configs.getObject("conf_User");
    if (quser === null) {
        throw "User not found.";
    }
    engine.log(`User Name: ${quser.getName()}`);
    return quser;
}

/**
 * データ項目からファイル / フォルダの ID を取得する
 * @return {String}  ファイル / フォルダの ID
 */
function getFileId() {
    const fileId = engine.findData(configs.getObject("conf_FileId"));
    if (fileId === null || fileId === "") {
        throw "File / Folder ID isn't set.";
    }
    return fileId;
}

/**
  * Google Drive API にファイル/フォルダの共有設定一覧を取得する GET リクエストを送信し、
  * 削除対象の共有設定の ID を返す
  * type が anyone または domain のものが削除対象
  * 削除対象の共有設定がない場合はエラー
  * @param {QuserView} quser  Google Drive に接続するユーザ
  * @param {String} fileId  ファイルの ID
  * @return {String} permissionId  削除する共有設定の ID
  */
function getPermissionIdToDelete(quser, fileId) {
    const url = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}/permissions`;
    const response = httpClient.begin()
        .googleOAuth2(quser, "Drive")
        .queryParam("fields", "permissions/id,permissions/type")
        .queryParam("supportsAllDrives", "true")
        .get(url);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status >= 300) {
        engine.log(responseStr)
        throw `Failed to get permission list. File:${fileId}, Status:${status}`;
    }
    const {permissions} = JSON.parse(responseStr);
    const permissionIds = permissions
        .filter(permission => permission.type === "anyone" || permission.type === "domain")
        .map(permission => permission.id);
    if (permissionIds.length === 0) {
        throw `No shared link to delete. File:${fileId}`;
    }
    // Web UI から設定できる anyone / domain の共有設定は 1 つのみ
    // 共有設定作成の API を直接叩けば anyone / domain の共有設定を複数作成できるが、ここでは考慮しない
    // もし複数ある場合は 1 つ目だけ削除
    return permissionIds[0];
}

/**
  * Google Drive API に DELETE リクエストを送信し、共有設定を削除する
  * @param {QuserView} quser  Google Drive に接続するユーザ
  * @param {String} fileId  ファイル ID
  * @param {String} permissionId  削除する共有設定の ID
  */
function deletePermission(quser, fileId, permissionId) {
    const url = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}/permissions/${permissionId}`;
    const response = httpClient.begin()
        .googleOAuth2(quser, "Drive")
        .queryParam("supportsAllDrives", "true")
        .delete(url);
    const status = response.getStatusCode();
    if (status >= 300) {
        engine.log(response.getResponseAsString())
        throw `Failed to delete permission. File:${fileId}, Permission:${permissionId}, Status:${status}`;
    }
}

    

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading