
Google ドライブ: 共有リンク作成 (Google Drive: Create Shared Link)
この工程は、Google ドライブ の指定ファイルやフォルダの共有リンクを作成します。すでにリンクを知っている全員、もしくはドメインで共有されているファイル/フォルダを指定した場合はエラーになります。
Configs:共通設定
- 工程名
- メモ
Configs
- C1: Google ドライブ に接続するユーザ (要アプリ管理権限) *
- C2: 共有するファイル/フォルダの ID *
- C3: 共有範囲 *
- C4: ロール (編集可能にするかどうか) *
- C5: 共有先のドメイン (共有範囲がドメインの場合は必須)
- C6: 共有先ドメインで検索可能にする
- C7: 共有したファイル/フォルダの表示 URL を保存するデータ項目
Notes
- C1 のユーザは、[アカウント設定]>[Google 連携]にて、Google ドライブと連携済みである必要があります
- ワークフロー基盤にて、Google 連携設定([システム設定]>[Google 連携])が必要です(要[システム管理権限])
- 「Google ドライブ: ファイルアップロード」「Google ドライブ: フォルダ作成」「Google スプレッドシート: ファイル作成」で作成されたファイル・フォルダの、共有リンクを作成可能です。
- 既存のファイルや他のソフトウェアが作成したファイルは共有リンクを作成できません。
- 認可スコープが https://www.googleapis.com/auth/drive.file であるためです。
Capture

See also
Script (click to open)
- 下記のスクリプトを記述した XML ファイルをダウンロードできます
- google-drive-permission-create.xml (C) Questetra, Inc. (MIT License)
- Professional をご利用であればファイルの内容を改変することでオリジナルのアドオンとして活用できます
main();
function main() {
const quser = getQuser();
const fileId = getFileId();
const sharedRange = configs.get("conf_SharedRange");
const role = configs.get("conf_Role");
const domain = configs.get("conf_Domain");
if (sharedRange === "domain" && domain === "") {
throw "Domain to share isn't set.";
}
const allowFileDiscovery = configs.getObject("conf_AllowFileDiscovery");
const urlDataDef = configs.getObject("conf_SharedUrlItem");
checkIfAlreadyShared(quser, fileId);
createPermission(quser, fileId, sharedRange, role, domain, allowFileDiscovery);
if (urlDataDef !== null) {
const sharedUrl = getWebViewLink(quser, fileId);
engine.setData(urlDataDef, sharedUrl);
}
}
/**
* 実行ユーザを取得する
* @return {QuserView} 実行ユーザ
*/
function getQuser() {
const quser = configs.getObject("conf_UserID");
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_FileIdsItem"));
if (fileId === null || fileId === "") {
throw "File / Folder ID isn't set.";
}
return fileId;
}
/**
* Google Drive API で共有設定の一覧を取得し、
* anyone / domain の共有設定がすでにある場合はエラー
* @param {QuserView} quser Google Drive に接続するユーザ
* @param {String} fileId ファイル / フォルダの ID
*/
function checkIfAlreadyShared(quser, fileId) {
const url = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}/permissions`;
const response = httpClient.begin()
.googleOAuth2(quser, "Drive")
.queryParam("fields", "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 alreadyShared = permissions
.some(permission => permission.type === "anyone" || permission.type === "domain");
if (alreadyShared) {
throw `The item (ID: ${fileId}) is already shared.`;
}
}
/**
* Google Drive API で共有設定をする
* create permission on Google Drive
* @param {QuserView} quser Google Drive に接続するユーザ
* @param {String} fileId ファイル / フォルダの ID
* @param {String} type 共有範囲
* @param {String} role ロール
* @param {String} domain 共有先ドメイン
* @param {String} allowFileDiscovery 検索可能にするかどうか
* @return {String} 共有リンク
*/
function createPermission(quser, fileId, type, role, domain, allowFileDiscovery) {
const json = {role, type};
if (type === "domain") {
json["domain"] = domain;
json["allowFileDiscovery"] = allowFileDiscovery;
}
const url = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}/permissions`;
const responsePermission = httpClient.begin()
.googleOAuth2(quser, "Drive")
.queryParam("supportsAllDrives", "true")
.body(JSON.stringify(json), "application/json; charset=UTF-8")
.post(url);
const status = responsePermission.getStatusCode();
if (status >= 300) {
engine.log(responsePermission.getResponseAsString())
throw `Failed to create permission. File:${fileId}, Status:${status}`;
}
}
/**
* Google Drive API でファイル / フォルダの表示 URL を取得する
* @param {QuserView} quser Google Drive に接続するユーザ
* @param {String} fileId ファイル / フォルダの ID
* @return {String} 表示 URL
*/
function getWebViewLink(quser, fileId) {
const url = `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}`;
const responseGet = httpClient.begin()
.googleOAuth2(quser, "Drive")
.queryParam("fields", "webViewLink")
.queryParam("supportsAllDrives", "true")
.get(url);
const getStatus = responseGet.getStatusCode();
if (getStatus >= 300) {
engine.log(responseGet.getResponseAsString())
throw `Failed to get item info. File:${fileId}, Status:${getStatus}`;
}
const resJson = JSON.parse(responseGet.getResponseAsString());
return resJson.webViewLink;
}
ピンバック: ワークフローから Google ドライブを活用する ~社外の人へファイルを送信する~ – Questetra Support