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
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.
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.
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
Allows to choose between Account Service and OAuth 2.0 authorization methods.
Name: authorizationMethod
Type: buttonsGroup
Mandatory: true
The email created for the service account, it shows up when Service Account authorization method is enabled.
Name: serviceAccountEmail
Type: text
Mandatory: true
The private key associated to the service account, it shows up when Service Account authorization method is enabled.
Name: privateKey
Type: password
Mandatory: true
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
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
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
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
The URL to configure in webhooks of your Google Drive App.
Name: webhooksUrl
Type: label
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.
You can make GET,POST,DELETE,PATCH requests to the Google Drive API like this:
const response = pkg.googledrive.api.get('/about?fields=user')log(JSON.stringify(pkg.googledrive.api.post("/files",{
body: {
mimeType: "application/vnd.google-apps.folder",
name: "test"
}
})));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));
}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);const response = pkg.googledrive.api.get('/drives/:driveId')const body = {
"content": "lorem ipsum"
}
const response = pkg.googledrive.api.patch('/files/:fileId/comments/:commentId?fields=id,content,author', body)pkg.googledrive.api.delete('/files/:fileId/comments/:commentId')Please refer to the documentation of the HTTP service for more information about generic requests.
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.
- HTTP Service
- Oauth Package
SLINGR is a low-code rapid application development platform that speeds up development, with robust architecture for integrations and executing custom workflows and automation.
This package is licensed under the Apache License 2.0. See the LICENSE file for more details.