The connector provides the capability to programmatically manage files and folders in the Google Drive.
This module supports Google Drive API v3.
Before using this connector in your Ballerina application, complete the following:
- Create a Google account
- Obtain tokens - Follow this link
To use the Google Drive connector in your Ballerina application, update the .bal file as follows:
Import the ballerinax/googleapis.drive module into the Ballerina projects shown below.
import ballerinax/googleapis.drive;
All the actions return a valid response or error. If the action is a success, then the requested resource is returned. If not, an error is returned.
Create a drive:ConnectionConfig with the OAuth2 tokens obtained, and initialize the connector with it.
configurable string refreshToken = ?;
configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshUrl = drive:REFRESH_URL;
drive:ConnectionConfig config = {
auth: {
clientId: clientId,
clientSecret: clientSecret,
refreshUrl: refreshUrl,
refreshToken: refreshToken
}
};
drive:Client driveClient = new (config);
-
Now you can use the operations available within the connector. Note that they are in the form of remote operations.
Following is an example on how to retrieve a file using the connector.
Get file with given file ID
public function main() returns error? { drive:File response = check driveClient->getFile(fileId); log:printInfo("Successfully retreived the file."); }
-
Use
bal run
command to compile and run the Ballerina program.