Skip to content

Upload Files to Google Driven Using curl

Create a project in GCP console

  1. Go to the URL https://console.cloud.google.com
  2. Login with desired google account
  3. Click On the project
  4. Click on new project
  5. 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 :::

  1. From the search bar, type APIs and Services, and Open
  2. Click + Enable APIs and services,
  3. In the API Library Search bar type ‘Google Drive API‘ and select it
  4. Click Enable

Create credentials for the API

  1. Click on Create credentials
  2. In Credential type Select Google Drive API and check User data
  3. In OAuth consent screen fill the App information
  4. Give app name
  5. User support email (Give the same email which is logged in)
  6. Do not upload app logo
  7. Give same email id again in Developer contact information
  8. Click save and continue
  9. Scopes is Optional (Do not make any changes in this section)
  10. Click Save and continue
  11. In OAuth Client ID
  12. Select Application type as Web application
  13. Give name for the web application client
  14. Add Authorised redirect URIs
    1. Click + Add URI
    2. Type: http://localhost
    3. Click Create
  15. In Your credentials
  16. Click on Download
  17. Click on Done

  1. From left side navigation section, click on OAuth consent screen
  2. Click Audience
  3. Scroll down to Test users
  4. Click Add user
  5. 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

CODE='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

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


Update file with new one

Get these values

FILE_ID='ADSJLJKSGKLBJ'
FILE_PATH='/path/to/File'
curl -X PATCH \
  "https://www.googleapis.com/upload/drive/v3/files/${FILE_ID}?uploadType=media" \
  -H "Authorization: Bearer ${ACCESS_TOKEN}" \
  -H "Content-Type: application/octet-stream" \
  --data-binary @"${FILE_PATH}"