Skip to content

slingr-stack/google-drive-package

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

Repo: https://github.com/slingr-stack/google-drive-package

This package allows direct access to the Google Drive API, through a Client ID OAuth 2.0 account; however, it provides shortcuts and helpers for most common use cases. Also, you can refer to the Google Drive Documentation for more information.

Some features available in this package are:

  • Authentication and authorization
  • Direct access to the Google Drive API
  • Helpers for API methods
  • Listener that catch incoming webhooks from Google Drive

Configuration

To use the Google Drive package, first you must create an app in the Google Developer Console then create a Google Cloud project for your Google Drive app, then if you plan to use Service Account authentication method, follow these instructions:

  • Enable the Admin SDK API in your Google Cloud project.
  • Create a service account and credentials and delegate domain-wide authority to it (assign ONLY the necessary scopes to your service - account) Click here for instructions.
  • Download the JSON file with the service account credentials to get the service account private key.

Otherwise, if you plan to use OAuth 2.0 authentication method:

  • Enable the Drive API in your Google Cloud project.
  • Create a Client ID OAuth 2.0 account.
  • Copy the Client ID and Client Secret of the package.

Scopes

Note that the client must have access to the drive resources. If you try to access to a resource that the user does not own the request will result in a 404 or 403 unauthorized error.

Configuration Parameters

If you have selected OAuth 2.0 authorization method, these are the field names to use the parameters with dynamic configuration.

Name (Dynamic Config param name) - Type

  • Client Id (clientId) - Text
  • Client Secret (clientSecret) - Text
  • State (state) - Text

Authorization Method

Allows to choose between Account Service and OAuth 2.0 authorization methods.

Name: authorizationMethod Type: buttonsGroup Mandatory: true

Service Account Email

The email created for the service account, it shows up when Service Account authorization method is enabled.

Name: serviceAccountEmail Type: text Mandatory: true

Private Key

The private key associated to the service account, it shows up when Service Account authorization method is enabled.

Name: privateKey Type: password Mandatory: true

Client ID

The ID for your client application registered with the API provider, it shows up when OAuth 2.0 authorization method is enabled.

Name: clientId Type: text Mandatory: true

Client Secret

The client secret given to you by the API provider, it shows up when OAuth 2.0 authorization method is enabled.

Name: clientSecret Type: password Mandatory: true

State

An opaque value to prevent cross-site request forgery. it shows up when OAuth 2.0 authorization method is enabled.

Name: state Type: text Mandatory: false

OAuth Callback

The OAuth callback to configure in your Google Drive App. it shows up when OAuth 2.0 authorization method is enabled.

Name: oauthCallback Type: label

Webhooks URL

The URL to configure in webhooks of your Google Drive App.

Name: webhooksUrl Type: label

Storage Value And Offline Mode

By default, the Service Account authorization method is used. When using this method, you can directly call the following method to retrieve the access token, without requiring any additional actions:

pkg.googledrive.api.getAccessToken();

This will return the access token, which will be securely stored in the application's storage and associated with a user by their ID.

If you have enabled the OAuth 2.0 authorization method, the same method is used. The difference is that the Google Drive package includes the &access_type=offline parameter, which allows the application to request a refresh token. This happens when calling the UI service (which should run during runtime, for example, by invoking the method within an action) to log in to the application.

The Google service will return an object containing both the access token and the refresh token. Each token will be stored in the app's storage (accessible via the Monitor), where you can view them encrypted and associated with the user by ID.

Javascript API

You can make GET,POST,DELETE,PATCH requests to the Google Drive API like this:

Gets information about the user, their Drive, and system functions.

const response = pkg.googledrive.api.get('/about?fields=user')

Create a folder

log(JSON.stringify(pkg.googledrive.api.post("/files",{
    body: {
      mimeType: "application/vnd.google-apps.folder",
      name: "test"
    }
  })));

Uploads a file

Google Drive API Documentation - Files: upload Mime Types

let file = sys.data.createRecord('archivos');
file.field('file').val({
  name: 'test1.txt',
  contentType: 'text/plain',
  content: 'dGVzdCBmaWxlIQ=='
});
file = sys.data.save(file);
try {
  log(JSON.stringify(pkg.googledrive.api.upload(file.field('file').id(), "test.txt", "16ulE6ZQP4gPUYaM8KYhSa9Cj9lWXXtak")));
} catch (e) {
  log("Full error: " + JSON.stringify(e));
  log("Short error description: " + JSON.stringify(e.message));
  log("Internal error: " + JSON.stringify(e.error));
  log("Error description: " + JSON.stringify(e.additionalInfo.body.description));
  log("Timestamp: " + JSON.stringify(e.additionalInfo.headers.date));
  log("Status code: " + JSON.stringify(e.additionalInfo.status));
  log("Body: " + JSON.stringify(e.additionalInfo.body));
  log("Headers: " + JSON.stringify(e.additionalInfo.headers));
}

Subscribes to file changes.

const body = {
  "id": "<use a unique UUID or any similar unique string>", // Your channel ID. Maximum length: 64 characters. 
  "type": "web_hook",
  "address": "https://mydomain.com/notifications", // Your receiving URL.
  //...
  "token": "target=myApp-myChangesChannelDest", // (Optional) Your changes channel token.
  "expiration": 1426325213000 // (Optional) Your requested channel expiration date and time.
};
const response = pkg.googledrive.api.post('/files/:fileId/watch', body);

Retrieves the metadata of a shared drive by ID.

const response = pkg.googledrive.api.get('/drives/:driveId')

Updates a comment

const body = {
  "content": "lorem ipsum"
}
const response = pkg.googledrive.api.patch('/files/:fileId/comments/:commentId?fields=id,content,author', body)

Deletes a comment

pkg.googledrive.api.delete('/files/:fileId/comments/:commentId')

Please refer to the documentation of the HTTP service for more information about generic requests.

Events

Webhook

Incoming webhook events are automatically captured by the default listener named Catch HTTP google drive events, which can be found below the Scripts section. Alternatively, you have the option to create a new package listener. For more information, please refer to the Listeners Documentation. Please take a look at the Google Drive documentation of the Webhooks for more information.

Dependencies

  • HTTP Service
  • Oauth Package

About Slingr

SLINGR is a low-code rapid application development platform that speeds up development, with robust architecture for integrations and executing custom workflows and automation.

More info about SLINGR

License

This package is licensed under the Apache License 2.0. See the LICENSE file for more details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6