Skip to content

Latest commit

 

History

History

gdrive-new-file-to-gsheet-rows

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Google Drive New File to Google Sheets Row

Use case

It's important usecase to track the files uploaded to a Google drive. This temaplate logs "FileID", "File Name", "Time", "Mime type" of the files uploaded to the Google drive in a Google sheet as rows.

Prerequisites

  • Google Account

Setting up Google Drive account

  1. Visit Google API Console, click Create Project, and follow the wizard to create a new project.
  2. Go to Credentials -> OAuth consent screen, enter a product name to be shown to users, and click Save.
  3. On the Credentials tab, click Create credentials and select OAuth client ID.
  4. Select an application type, enter a name for the application, and specify a redirect URI (enter https://developers.google.com/oauthplayground if you want to use OAuth 2.0 playground to receive the authorization code and obtain the access token and refresh token).
  5. Click Create. Your client ID and client secret appear.
  6. In a separate browser window or tab, visit OAuth 2.0 playground, select the required Google Drive scopes, and then click Authorize APIs.
  7. When you receive your authorization code, click Exchange authorization code for tokens to obtain the refresh token and access token.
  8. Domain used in the callback URL needs to be registered in google console as a verified domain. https://console.cloud.google.com/apis/credentials/domainverification (If you are running locally, provide your ngrok url as to the domain verification) Then you will be able to download a HTML file (e.g : google2c627a893434d90e.html). Copy the content of that HTML file & provide that as a config (domainVerificationFileContent) to Listener initialization.

Setting up for Google Sheet account

  1. Visit Google account and create a connected app by visiting Google cloud platform APIs and Services.
  2. Click Library from the left side menu.
  3. In the search bar enter Google Sheets.
  4. Then select Google Sheets API and click Enable button.
  5. Complete OAuth Consent Screen setup.
  6. Click Credential tab from left side bar. In the displaying window click Create Credentials button Select OAuth client Id.
  7. Fill the required field. Add https://developers.google.com/oauthplayground to the Redirect URI field.
  8. Get clientId and secret. Put it on the config(Config.toml) file.
  9. Visit https://developers.google.com/oauthplayground/ Go to settings (Top right corner) -> Tick 'Use your own OAuth credentials' and insert Oauth ClientId and secret.Click close.
  10. Then,Complete Step1 (Select and Authotrize API's)
  11. Make sure you select https://www.googleapis.com/auth/drive & https://www.googleapis.com/auth/spreadsheets Oauth scopes.
  12. Click Authorize API's and You will be in Step 2.
  13. Exchange Auth code for tokens.
  14. Copy Access token and Refresh token. Put it on the config(Config.toml) file.

Configuration

Create a file called Config.toml at the root of the project.

Config.toml

[<ORG_NAME>.gdrive_new_file_to_gsheet_rows]
driveClientId = "<CLIENT_ID>"
driveClientSecret = "<CLIENT_SECRET>"
driveRefreshToken = "<REFRESH_TOKEN>"

[<ORG_NAME>.gdrive_new_file_to_gsheet_rows.spreadsheetConfig]
clientId = "<CLIENT_ID>"
clientSecret = "<CLIENT_SECRET>"
refreshToken = "<REFRESH_TOKEN>"
refreshUrl = "<REFRESH_URL>"

> Note: Here REFRESH_URL for Google Sheets API is `https://www.googleapis.com/oauth2/v3/token`

## Testing
Run the Ballerina project created by the integration sample by executing `bal run` from the 

Now you can add new file to google drive and check new rows in google sheet. You can check the metadata in rows to verify the file added.