1. Setting Up Google Drive API
To set up the Google drive API, first we need to create a project in console screen – Google API Console – Sign in to the Google Account you want the drive to be integrated – APIs & Services – APIs & Services – Test Google Drive A… – Google API Console
Create a project to start enabling the APIs and services. Here by Default its Quick Start. But to create a project you have an option there at the place where QuickStart resides now.
Clicking on that the below screenshot appears. Select the organization – Innovalleyworks.com
Click on New Project. The below screen appears. Fill the project name and click CREATE
Once created, you will have a pop up on right side to select that project you want to integrate with Google drive. Please note that client and client secret will differ with each project you create.
After selecting the project, you will find the project name is updated near Google APIs icon. Now click on Enable APIs and Services.
Scroll to find the Google Drive API in the listed API library. And click on it
Click on Enable button
Try this App button will lead you to API Documentation page for guiding you with API request parameters and response expected – API Reference | Google Drive API | Google Developers
Once your Google Drive API is enabled you will get the below screen
It’s time to create credentials for the Application. Click on Create Credentials on top right. The below screen appears
Click on Configure Consent Screen to first configure OAuth Consent screen before generating credentials. Give external to configure test users to access the application and click on CREATE. If testing done as part of organisation internally only then INTERNAL
This will redirect to app registration page. Fill in the details. Filling mandatory details and Save and Continue will be enough to configure it
Fill in if any scopes required
Click on Add Users to create Test Users
Once all done, all details will be listed as summary.
Click on the Credentials on left above OAuth Consent screen option in the list
Click on Create Credentials, you will see three options listed. Select each to get the API key and client ID.
For Client ID generation, below details need to be added. Redirect UI can be of any domain as it’s just used to create Code every time to generate access token. Click on CREATE
Click on Create Credentials, you will see three options listed. Select each to get the API key and client ID.
3. Generate Access Token Using Postman
Use the below CURL with your details of API and generate the access token – Please note that access token will have to be generated with new code every time using above step 2 when it expires
curl –location –request POST ‘https://accounts.google.com/o/oauth2/token’ \
–form ‘grant_type=”authorization_code”‘ \
–form ‘code=””‘ \
–form ‘client_id=” “‘ \
–form ‘client_secret=”=” “‘ \
–form ‘redirect_uri=”=”=” “‘
Sample Response:
{
“access_token”: “ya29.a0AfH6SMAD5-yA-I-SwE8Z00xV0RhFqKrIUhsByWg3xdlL2DHd7iisvEKM6s4ba-fSu7mUdOftAfbcouqwEVwJmdgxQMktfIm6Z_swHJdclum03pFozcJucvMsuvjS8_c6VilL7CeeD_Eew8TtbwxUGik18VSZ”,
“expires_in”: 3588,
“scope”: “https://www.googleapis.com/auth/drive”,
“token_type”: “Bearer”
}
“Code” can be generated with beow URL
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A//www.googleapis.com/auth/drive.metadata.readonly&
access_type=offline&
include_granted_scopes=true&
response_type=code&
state=state_parameter_passthrough_value&
redirect_uri=https%3A//login.salesforce.com&
client_id=190441262821-aniosp2ojs6jc6ntoqk4fsu31j1d5mj2.apps.googleusercontent.com
4. File Upload to Google Drive
Upload Type – Media
Note: I selected my media using Body -> binary -> Select File -> Choose a file from desktop
Content Type – Type of the media
Authorization – Use the access token from above to your Authorization Bearer
curl –location –request POST ‘https://www.googleapis.com/upload/drive/v3/files?uploadType=media’ \
–header ‘Content-Type: image/png’ \
–header ‘Authorization: Bearer ya29.a0AfH6SMBU5SCFX-y758KASR-Nj2HIhH7Lo94Z4gB4yl1cX63o3RnlMDBsoy0BcGlRbsW0dBTAB7gcSlH_rri4VQK-ns0BeaNZ8OFUpgVN82RrdV34Su6N5fTTxmchzKcGXHnsZ0TK8ZjrCQlEv_WNgfUD1TTR’ \
–data-binary ‘@/C:/Users/Downloads/Screenshot 2021-03-04 at 11.17.37 AM.png’
Upload Type – Multipart form data (Files)
Note: I selected my media using Body -> binary -> Select File -> Choose a file from desktop
Content Type – Type of the file
Authorization – Use the access token from above to your Authorization Bearer
curl –location –request POST ‘https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart’ \
–header ‘Content-Type: application/pdf’ \
–header ‘Authorization: Bearer ya29.a0AfH6SMD4xbqcmPc574F2TZhUaiSzA_B3t2B50-t0zUWCOjBH-dsI20TXEERYKiq76T-o3r0UM3o9k9OZSEp0DXnw1kUpCYXpz-UejGFFkZ0vTmdBwgYCSIZXkzPWXxbmQFzKfEeNLuY9jkDk-trEvsO9X8oykg’ \
–data-binary ‘@/C:/Users/Downloads/Test.pdf’
We are Inno Valley Works , We are a passionate team of developers, best thinkers and consultants who can solve anything and everything.
With our highly engaging team, you can easily bring the vision to all your business ventures come true.
We have team, put your problem, get your solution