This guide outlines the steps to upload Excel files to Google Drive using ColdFusion. We’ll cover the requirements, how to generate OAuth 2.0 credentials, and the necessary code implementation to facilitate file uploads seamlessly.

6-gdrive-Photoroom

Uploading an Excel File to Google Drive Using ColdFusion

dastek logo

Requirements :

      1. ColdFusion Server
      2. Google Drive API Credentials: Obtain OAuth 2.0 credentials from the Google Developer Console.
      3. Excel File: The file you intend to upload

Steps to Generate OAuth 2.0 Credentials :

      1.  Visit the Google Cloud Console:
      2.  Log in with Your Google Account:
          • Use your Google email to log in.
      3.  Create a new Project:
          • Click on the project dropdown at the top of the page.
          • Select “New Project” and follow the prompts to create and name your project.
      4. Enable the Google Drive API:
          • In the left menu, go to API & Services > Library.
          • Search for “Google Drive API” and select it.
          • Click Enable to enable the API.
      5. Configure the OAuth Consent Screen:
          • In the left menu, go to API & Services > OAuth consent screen.
          • Select External or Internal based on your needs and follow the prompts to configure the consent screen. Fill in the required fields and save your settings.
      6. Publish the OAuth Consent Screen:
          • After configuring, click Publish to make it live.
      7. Create OAuth 2.0 Credentials:
          • Go to API & Services > Credentials.
          • Click Create Credentials and select OAuth 2.0 Client IDs.
          • Choose Web application as the application type.
          • Enter a name for your client ID.
          • In the Authorized redirect URIs section, add the redirect URI: https://developers.google.com/oauthplayground/.
          • Click Create and note down the Client ID and Client Secret.
      8. Get a Refresh Token Using OAuth Playground:
          • Go to https://developers.google.com/oauthplayground/
          • Click on the OAuth 2.0 configuration icon (gear icon) and check the box for Use your own OAuth credentials.
          • Enter the Client ID and Client Secret you obtained earlier and close the settings.
      9. Authorize and Obtain Tokens:
          • In OAuth Playground, select the Drive API v3 scope by choosing https://www.googleapis.com/auth/drive.
          • Click Authorize APIs and go through the authentication process.
          • After authentication, you’ll be redirected back to OAuth Playground, where you will see the option to Exchange authorization code for tokens.
          • Click on it to get the access and refresh tokens.

Step 1: Set Up OAuth 2.0 Token Refresh:

Obtain an access token to authenticate API requests to Google Drive.

Explanation:

        • OAuth 2.0 Authentication: Google Drive API uses OAuth 2.0 for authentication. An access token is needed to interact with the API. This token is short-lived, so you use a refresh token to get a new access token when it expires.
        • Token Refresh URL: https://oauth2.googleapis.com/token.
        • Client Credentials: Client ID and Client Secret from your Google Cloud project.
        • Refresh Token: Obtained during the OAuth 2.0 authorization process.

Explanation of Code:

                • <cfhttp>: Sends a POST request to the token URL with the necessary parameters to get a new access token.
                • <cfset tokenResponse>: Parses the JSON response to extract the access token.
                • Error Handling: Captures and reports any issues during the token refresh process.

Step 2: Create Metadata for the File Upload:

Define the metadata for the file you want to upload to Google Drive.

Explanation:

            • Folder ID: The ID of the Google Drive folder where the file will be placed.
            • File Path: The local path to the file you want to upload.
            • File Name: The name you want to give to the file in Google Drive.

Explanation of Code:

            • folderId: Specifies the Google Drive folder where the file will be uploaded.
            • filePath: Path to the file on your local machine.
            • metadata: JSON formatted metadata used by Google Drive API to identify the file and its location.

Step 3: Initiate the File Upload:

Get a resumable upload URL from Google Drive.

Explanation:

        • Resumable Uploads: Google Drive supports resumable uploads for large files, allowing you to upload the file in chunks.
        • Metadata Request: A POST request with metadata is sent to obtain a resumable upload URL.

Explanation of Code:

          • <cfhttp>: Sends a POST request to start the upload process. The response includes a URL for resumable uploads.
          • resumableUrl: The URL where the file content will be uploaded.

Step 4: Upload the File Content:

Upload the actual file content to the resumable URL.

Explanation:

            • Read File Content: Load the file content from the local file system.
            • Upload File: Send the file content to the resumable URL.

Explanation of Code:

          • fileContent: Reads the binary content of the file to be uploaded.
          • <cfhttp>: Sends a PUT request to upload the file to the Google Drive API.

Summary

      1. Refresh OAuth Token: Obtain a valid access token using your refresh token.
      2. Create Metadata: Define the metadata for the file, including the file name and folder location.
      3. Initiate Upload: Request a resumable upload URL from Google Drive.
      4. Upload File: Upload the file content to the resumable URL

Trust and Worth

Our Customers

We are having a diversified portfolio and serving customers in the domains namely Sports Management, Online Laundry System, Matrimonial, US Mortgage, EdTech and so on.

Would you like to start a project with us?

DAStek team would be happy to hear from you and would love to turn your ‘Imaginations to Reality’.