Google Drive is a storage service available for Google users and allows you to store all kinds of files. All users have 15GB for free after creating their Google account, and all you need to do is log in at https://drive.google.com and then upload your files inside.
For advanced users (developers), there is an API available to programmatically performs actions like Create a folder, add a file, search files, etc... It can be useful in some cases:
- Backup your database and upload it to your drive
- Set a Cron job to look up inside a folder and upload the files inside
- Receive an email when you reach the storage threshold
- Delete all files older than a specific date
Here is the goal we want to achieve: We have a
.jpg file on our server, and we want to upload it into our Google Drive inside a folder named
Picture. If the folder doesn't exist, we will create it, and finally, when the file is successfully updated, delete the file on the server.
Get API Credentials
The first step is to get our Google Drive credentials which are:
- A Client Id and a Client Secret
- A redirect URI and refresh token
You need to have a Google account for the steps below:
- Go to https://console.cloud.google.com/cloud-resource-manager and click on the button "Create Project". Give a name to the project, click on "Create" to submit, and when for the creation to complete.
2. Once the project is created, select it. You will be redirected to the console dashboard. On the sidebar menu, click on the menu "APIs & Services".
Locate the button labeled "ENABLE APIS AND SERVICES" and click on it.
3. You will be redirected to a page that lists all the Google APIs. Search for Google Drive API and click on it in the results list. On the next page, click on the button "Enable", you will be redirected to a page when the API will be enabled.
4. On the new page, click on the menu in the sidebar labeled "Credentials". On the next page, locate the drop-down button labeled "CREATE CREDENTIALS" click on it and select the drop-down menu labeled "OAuth client ID"
5. On the new page, click on the button "CONFIGURE CONSENT SCREEN" then check "External" for the User Type. You can select "Internal" if the account you use is inside an organization which is not my case. Click on the button "Create"
On the new page, we have a page with 4 steps.
In step 1, give your application the name and select your email address as the value for the input labeled "User support email". Also, give your email address as value for input labeled "Developer contact information". You can ignore other inputs since they aren't mandatory. Click on "Save and Continue"
On step 2, no change to do, so click on "Save and Continue."
In step 3, add a Test user with your email address. Note that you can add up to 100. Click on "Save and Continue"
Step 4 is just a summary of the previous steps. Click on the button "BACK TO DASHBOARD."
6. On the new page, click on the menu in the sidebar labeled "Credentials". On the next page, locate the drop-down button labeled "CREATE CREDENTIALS" click on it and select the drop-down menu labeled "OAuth client ID"
On the next page, select "Web Application" as the value of the input labeled "Application type,"
Give a name to our Web Application.
In the section "Authorized redirect URIs," click on "ADD URI" and fill the input with this value: https://developers.google.com/oauthplayground
Click on "CREATE" to submit, and hooray ? , we have our Client ID and Client Secret.
At this step, we already have:
- Client ID ✅
- Client Secret ✅
- Redirect URI ✅ (https://developers.google.com/oauthplayground)
- Refresh Token ❌
7. Let's get the refresh token.
You will be redirected to our Google Account to authorize the app to access our Google Drive.
Authorize, and you will be redirected to a page with the content below:
When the request is completed, you will have your refresh token ?.
We have everything to start interact with Google Drive API. We will use the Node.js project already set up with Typescript that we created throughout this previous tutorial so, clone the repository at this link then go inside.
At the root folder, create a directory named "public" and copy a picture file inside, which will be uploaded to Google Drive. I took this picture on Unsplash by SpaceX.
Setup environment variables
We need the credentials we created previously to interact with Google Drive API and to avoid using them in raw, we need to load them from a configuration file. We will use the dotenv package; we also need the Node.js SDK for Google API. So, inside the terminal at the project root, run the code below to install them:
Create a file named
.env then add the content below:
src/index.ts and type the code below save and run the code
yarn start to see the result:
Interact with Google Drive API
We will create a file named
googleDriveService which 4 methods:
- createDriveClient(): create a client with the credentials generated before, and we will use it to make calls to Google Drive API
- searchFolder(): Search a folder inside Google Drive.
- createFolder(): Create a new folder inside Google Drive
- saveFile(): Create a new file in a folder inside Google Drive
Now let's use the GoogleDriveService in our
index.ts file so replace the content by the one below:
yarn start to start the application and see the result:
You can found the source code of this tutorial at this link.
I hope you find it helpful; follow me on Twitter for more content.