Goal: To upload all the files that has been uploaded to Salesforce org via Notes & Attachments to the connected Google Drive with seperate folders for each object.
Description:
- Apex Trigger file with name "uploadFileToGdriveTriggerOnContentVersion.trigger" - This will be executed whenever a "ContentVersion" has been created into the salesforce system and It'll call a trigger handler class named "uploadFileToGdrive.cls"
- Apex Class for trigger handler with name "uploadFileToGdrive.cls" - This class holds the over all folder validation, folder creation, file upload and renaming processes.
- Apex Class for mimeType conversion helper with name "MIMEHelper.cls" - This class is a helper to convert the file types info mimeType which is passed into the API calls to setup the file type in google drive
- Schedule Apex Class for getting new token every 1 hour using refresh token with name "getNewTokenForGDrive.cls" - Since the access token has a validity of 60 mins, schedule this class for every 1 hour which will refresh the access token.
- Visual Force Page with the name "gDrive.page" - It acts as a landing page for the initial API call to get the access token & refresh token for the 1st time.
- Apex class with the name "connectGdrive.cls" = It is the helper for the VF Page to catch the Authorization ID and make another API callout to get the access token & refresh token and store them to a custom object.
- Create a dummy VF page named closePage for redirection after getting the Authorization Code.
Note: Using a Custom Object "gDriveTokens__c" with two custom fields "Access_Token__c" & "Refresh_Token__c"
Hit this link once to get the access token (change the redirect_uri with your VF page link). Once refresh token are received, it will be stored in the custom object and it is valid for 7 days:
Note: Before this step you need your own Client Id & Client Secret to access the Google Drive API. Ref: https://developers.google.com/drive/api/guides/about-sdk