Google Drive: File Permissions, Add Reader
Google Drive: File Permissions, Add Reader
Creates a Reader permission. A Google account email address is required. It is also possible to retain the Permission ID.
Configs
  • A: Select Authorization Setting (OAuth2) *
  • B: Set File ID (fileId) *#{EL}
  • C: Set Email Address to be Granted *#{EL}
  • C2: Set “group” to Grant to Group Addresses (default is “user”)#{EL}
  • D: Set Message in Notification Email#{EL}
  • E: Select STRING DATA that stores PermissionId (update)
Script (click to open)
// GraalJS Script (engine type: 2)
/*
- This request requires authorization with at least one of the following scopes:
    - https://www.googleapis.com/auth/drive
    - https://www.googleapis.com/auth/drive.file (only files created by the app)
- Supports both My Drives and shared drives. Shared-with-me are also referable.
- No error will occur even if executed while it has already been created.
- No error will occur even if granted to a non-existent address.
- ファイル権限を持つユーザにより、OAuth認可される必要があります。(以下のうち少なくとも1つ)
    - https://www.googleapis.com/auth/drive
    - https://www.googleapis.com/auth/drive.file
- マイドライブと共有ドライブの両方をサポートします。共有アイテムも参照可能です。
- すでに追加済みの状態で実行してもエラーにはなりません。
- 実在しないアドレスに付与してもエラーにはなりません。
- "FileID" in URL
    - https://docs.google.com/spreadsheets/d/FILEID/edit#gid=0
    - https://docs.google.com/document/d/FILEID/edit
    - https://drive.google.com/file/d/FILEID/view
- To Get "ClientId" and "Secret"
    - Access to https://console.developers.google.com/apis/credentials
- OAuth Setting sample
    - "Authorization Endpoint URL"
        - https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
    - "Token Endpoint URL"
        - https://accounts.google.com/o/oauth2/token
    - "Scope"
        - https://www.googleapis.com/auth/drive.file
- Error Log sample (wrong fileId or Scope)
    - AutomatedTask UnexpectedResponseError: 404
        - { "error": {
        - "errors": [ {
            - "domain": "global",
            - "reason": "notFound",
            - "message": "File not found: 1lKhxxxxcQ2Fxxxx1d0lxxxxCftexxxxQ.",
            - "locationType": "parameter",
            - "location": "fileId"
        - } ],
        - "code": 404,
        - "message": "File not found: 1lKhxxxxcQ2Fxxxx1d0lxxxxCftexxxxQ."
        - } }
*/

//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strOauthSetting       = configs.get( "OAuth2ConfA" );   // required
const strFileId             = configs.get( "StrConfB" ) + ""; // required
const strEmail              = configs.get( "StrConfC" ) + ""; // required
const strGroupFlag          = configs.get( "StrConfC2") + "";
const strMessage            = configs.get( "StrConfD" ) + "";
const strPocketPermissionId = configs.getObject( "SelectConfE" );

if( strFileId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {FileId B} not specified \n" );
}
if( strEmail === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Email C} not specified \n" );
}


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)


//// == Calculating / 演算 ==

// prepare request1
// ref) https://developers.google.com
//            /drive/api/v3/reference/permissions/create
let request1Obj = {};
    request1Obj.role = "reader";
    request1Obj.type = "user";
    if( strGroupFlag === "group" ){
      request1Obj.type = "group";
    }
    request1Obj.emailAddress = strEmail;
let uri1     = "https://www.googleapis.com/drive/v3/files/" +
                strFileId + "/permissions";
const token  = httpClient.getOAuth2Token( strOauthSetting );
let request1 = httpClient.begin(); // HttpRequestWrapper
    request1 = request1.bearer( token );
    request1 = request1.queryParam( "supportsAllDrives", "true" );
    if( strMessage !== "" ){
      request1 = request1.queryParam( "emailMessage", strMessage );
    }
    request1 = request1.body( JSON.stringify( request1Obj ), "application/json" );
engine.log( " AutomatedTask ApiRequest Prepared" );

// post request1
const response1     = request1.post( uri1 );       // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest Start: " + uri1 );
const response1Code = response1.getStatusCode() + ""; // (primitive string)
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
                    response1Code + "\n" + response1Body + "\n" );
}

// parse response1
/* engine.log( response1Body ); // debug
response sample
{
 "kind": "drive#permission",
 "id": "01139999047299990829",
 "type": "user",
 "role": "reader"
}
*/
const response1Obj    = JSON.parse( response1Body );
let   strPermissionId = response1Obj.id + "";
engine.log( " AutomatedTask ApiResponse NewPermissionId: " + strPermissionId );


//// == Data Updating / ワークフローデータへの代入 ==
if( strPocketPermissionId !== null ){
  engine.setData( strPocketPermissionId, strPermissionId );
}


} //////// END "main()" /////////////////////////////////////////////////////////////////

Download

2020-10-06 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-drive-file-permissions-add-reader/
The Addon-import feature is available with Professional or Enterprise edition.

Notes

Capture

Creates a Reader permission. A Google account email address is required. It is also possible to retain the Permission ID.

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: