Upload Files to Google Driven Using curl
Create a project in GCP console
- Go to the URL https://console.cloud.google.com
- Login with desired google account
- Click On the project

- Click on new project
- Give the name of your choice and click create
Project is now created
Enable the Google Drive API
:::info Ensure the desired project is selected before enabling the API :::
- From the search bar, type APIs and Services, and Open
- Click
+ Enable APIs and services, - In the API Library Search bar type ‘Google Drive API‘ and select it

- Click Enable
Create credentials for the API
- Click on
Create credentials
- In Credential type Select Google Drive API and check User data
- In OAuth consent screen fill the App information
- Give app name
- User support email (Give the same email which is logged in)
- Do not upload app logo
- Give same email id again in Developer contact information
- Click save and continue
- Scopes is Optional (Do not make any changes in this section)
- Click Save and continue
- In OAuth Client ID
- Select Application type as Web application
- Give name for the web application client
- Add Authorised redirect URIs
- Click
+ Add URI - Type:
http://localhost - Click Create
- Click
- In Your credentials
- Click on Download
- Click on Done
Configuring OAuth consent screen
- From left side navigation section, click on OAuth consent screen
- Click Audience
- Scroll down to Test users
- Click Add user
- Give same email ID and click save
Generating Authorization code
From credentials file extract client-id and client secret, create variables as below
CLIENT_ID='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
CLIENT_SECRET='BBBBBBBBBBBBBBBBBBBBBBBBB'
REDIRECT_URI='http://localhost'
RESPONSE_TYPE='code'
ENDPOINT='https://accounts.google.com/o/oauth2/v2/auth'
SCOPE='https://www.googleapis.com/auth/drive'
Run the URL in browser
https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/drive&response_type=${RESPONSE_TYPE}&redirect_uri=${REDIRECT_URI}&client_id=${CLIENT_ID}&access_type=offline&prompt=consent
:::warning In the URL Variables are used, before running replace it with exact values :::
Login with the same google email ID, and authorize the request in the browser, the request will redirect it http://localhost
Make a note of redirect URL which has the authorization code
http://localhost/?code=XXXXXXXXXXXXXXXXXXXX&scope=https://www.googleapis.com/auth/drive
Generating refresh token
curl -d "code=${CODE}" \
-d "client_id=${CLIENT_ID}" \
-d "client_secret=${CLIENT_SECRET}" \
-d "redirect_uri=${REDIRECT_URI}" \
-d "grant_type=authorization_code" \
https://oauth2.googleapis.com/token
:::info Authorization code can be used once to generate refresh token and access token :::
Generating access token
curl -d "refresh_token=${REFRESH_TOKEN}" \
-d "client_id=${CLIENT_ID}" \
-d "client_secret=${CLIENT_SECRET}" \
-d "grant_type=refresh_token" \
https://oauth2.googleapis.com/token
:::info Refresh token can be used multiple times to generate access tokens :::
Upload Files using curl
Create metadata file
{
"name": "new.png",
"description": "Monthly sales report",
"mimeType": "image/png",
"parents": ["FOLDER-ID"]
}
Give File name and description. Give file mime type and folder id.
**To find your folder ID**
1. Launch a browser and login to https://drive.google.com 2. Navigato to the folder 3. Copy the URL when the folder is opened 4. URL will have the folder id[https://drive.google.com/drive/folders/](https://drive.google.com/drive/folders/16bUT4vtAURnv6Te1XKFc583MTmo4qILq)FFFFFFFFFFFFFFFFFFFFFFF 5. The FFFFFFFFFF will be the folder ID**To find the right mimeType**
| File Type | Extension | MIME Type | | --- | --- | --- | | Plain Text | .txt | “`text/plain`“ | | CSV File | .csv | “`text/csv`“ | | JSON file | .json | “`application/json`“ | | XML | .xml | “`application/xml“ or “text/xml`“ | | PDF Document | .pdf | “`application/pdf`“ | | HTML file | .html, .htm | “`text/html`“ | | JPEG image | .jpg or jpeg | “`image/jpeg`“ | | PNG image | .png | “`image/png`“ | | MP3 or WAV audio file | .mp3 or .wav | “`audio/mpeg`“ or “`audio/wav`“ | | Video files | .avi or .mp4 | “`video/x-msvideo`“ or “`video/mp4`“ | | ZIP archive file | .zip | “`application/zip`“ | | TAR archive file | .tar | “`application/x-tar`“ | | RAR archive | .rar | “`application/x-rar-compressed`“ | | Shell Script | .sh | “`application/x-sh`“ |curl -X POST -L \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-F "metadata=${METADATA};type=application/json;charset=UTF-8" \
-F "file=@"/file/path";type=${MIME}" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
The file will get uploaded