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.
- Google Account
- Visit Google API Console, click Create Project, and follow the wizard to create a new project.
- Go to Credentials -> OAuth consent screen, enter a product name to be shown to users, and click Save.
- On the Credentials tab, click Create credentials and select OAuth client ID.
- 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).
- Click Create. Your client ID and client secret appear.
- In a separate browser window or tab, visit OAuth 2.0 playground, select the required Google Drive scopes, and then click Authorize APIs.
- When you receive your authorization code, click Exchange authorization code for tokens to obtain the refresh token and access token.
- 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.
- In case if you failed to verify or setup, Please refer the documentation for domain verification process https://docs.google.com/document/d/119jTQ1kpgg0hpNl1kycfgnGUIsm0LVGxAvhrd5T4YIA/edit?usp=sharing
- Visit Google account and create a connected app by visiting Google cloud platform APIs and Services.
- Click Library from the left side menu.
- In the search bar enter Google Sheets.
- Then select Google Sheets API and click Enable button.
- Complete OAuth Consent Screen setup.
- Click Credential tab from left side bar. In the displaying window click Create Credentials button Select OAuth client Id.
- Fill the required field. Add https://developers.google.com/oauthplayground to the Redirect URI field.
- Get clientId and secret. Put it on the config(Config.toml) file.
- 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.
- Then,Complete Step1 (Select and Authotrize API's)
- Make sure you select https://www.googleapis.com/auth/drive & https://www.googleapis.com/auth/spreadsheets Oauth scopes.
- Click Authorize API's and You will be in Step 2.
- Exchange Auth code for tokens.
- Copy Access token and Refresh token. Put it on the config(Config.toml) file.
Create a file called Config.toml
at the root of the project.
[<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.