Mount Google Drive using GDrive on Linux Server with Own OAuth Credentials

Last modified on May 30, 2020 2 min read

In this article, I’m going to share how to mount Google Drive using GDrive with your own OAuth credentials. So, let’s start:

Table of Contents

  1. Install Go on Your Machine
  2. Create Own Google Credentials
  3. Download GDrive & Set Credentials
  4. Build the Project
  5. Upload & Test GDrive on Server
  6. File Upload Test
  7. Make Schedule Backup

Step 1 : Install Go on Your Machine

At first, we have to install Go Language on our machine (Server, macOS, Windows etc.). I’ve written an article about Go installation for RHEL/CentOS server.

Step 2 : Create Own Google Credentials

To create your own credentials, follow these steps:

  • Go to Google API Console
  • Create a new project
  • Enable Google Drive API
  • Configure Consent Screen
  • Create credentials by choosing OAuth client ID

This link may help you to create your own Google credentials.

Step 3 : Download GDrive & Set Credentials

We need to import gdrive package using this command:

go get

The gdrive binary should now be available at $GOPATH/src/

From the binary folder, open handlers_drive.go file from the root of the project directory and set your own credentials at 17 & 18 lines.

const ClientId = "367116221053-7n0v**"
const ClientSecret = "1qsNodXN*****jUjmvhoO"

Step 4 : Build the Project

We’re about to finish. Let’s build the project. Run this command to build the project for Linux:

go build

After building the project, you’ll find a new file called “gdrive” on the root of the project folder. We’ll upload this file on our server.

Note for Windows users: For Windows users, you need to change go GOOS environment variable. To check current GOOS value, run this command:

go env

If you see the value like GOOS=windows, you have to change to set GOOS=linux. To do this, open PowerShell and type this command:

$env:GOOS = "linux"

After that, you can check go env again and then run go build to build the project. I recommend to use PowerShell for Windows users.

Note For macOS Users: At the time of building the project if you face error, you can try by changing GOOS env. To change GOOS env from macOS run this command:

export GOOS=linux

Now you can try to build the project using go build command.

Credit: Thanks Yuom Theara for sharing the macOS issue and solution. πŸ™‚

Step 5 : Upload & Test GDrive on Server

We’ve already built GDrive with our own credentials. It’s time to test. I’m testing on CentOS 8. Let’s upload the generated gdrive file on our server.

Move GDrive to your server’s bin folder. The bin path of CentOS is: /usr/sbin.

sudo mv gdrive /usr/sbin/gdrive

We need to set file permission. Run this command to set permission:

# CentOS
sudo chmod 700 /usr/sbin/gdrive
# OtherOS
sudo chmod 700 /server-bin-path/gdrive

Run gdrive list command to start the authentication process. You will see a link like this:****

Copy and paste the link in the browser. Accept the permission and you will get a verification code. It may show unverified, but just click on advanced and click goto <your app name>(unsafe).

Copy the verification code and paste:

Enter verification code: 4/9gKYAFAJ326XIP6J42t35****

We have installed & setup GDrive and connected it to our Google Drive. Now we are able to use GDrive commands. πŸ™‚

Step 6 : File Upload Test

Let’s try to upload a file from server to Google Drive.

 # upload to google drive root folder
gdrive upload

# upload to a specefic folder
gdrive upload --parent 1dYSp4NcaRTykSebFt-UoAadszRhMQciX

File successfully saved to drive:

Step 7 : Make Schedule Backup

To make schedule backup, just add a cron job. Open cronjob file by typing this:

sudo crontab -e

Now add this line to take backup daily at 12 AM:

0 0 * * * /var/www/domain-path/public_html/ >/dev/null 2>&1
Done…! I hope this article will help you. πŸ™‚

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write article on Laravel, Linux server, modern JavaScript and more on web development.


42 Replies to “Mount Google Drive using GDrive on Linux Server with…”

  1. Great post, been looking for a good way to attached google drive to my centos box running CWP7, for the means to backup, because CWP7 has a terrible backup service…

  2. Great post, thank you very much! I’m not very experienced with this kind of procedure (compiling sources, etc), but, the way you described it, I feel confident to try.
    In most of the computers that I use, I still have acess to GoogleDrive API for gdrive, since I set gdrive on them a long time ago.
    I’m having troubles with Gdrive only in a new Windows 10 computer that I’ve started to use last January. Isn’t there a way of copy the access credentials from the other computers to the new one?

    1. Hi Manoel Guimaraes, thank you so much for trying this method. If you face any issue, let me know. I’ll try to sort.

      Yeah, It’s possible to find access credentials from the old device. You’ll find the credentials in the root path of your device. The gdrive config file on CentOS is: /root/.gdrive/config.json or /username/.gdrive/config.json. I didn’t test on Windows. But I think you may find the config file at C:\Users\USERNAME\.gdrive.

  3. Great work! thank you so much for your post! I have stacked for about a month to transfer large files to google drive. This post is LIFE-SAVER!!!!!
    One thing I would like to ask? I’m new to crontab. I don’t understand what this part means: /var/www/domain-path/public_html/ How can I sync only one folder in ubuntu server to one folder in google drive? BTW, I’m not the root user but have the root password.

    1. Hi Yu An. Thank you so much for trying this method.

      Crontab is a time-based job scheduler. You can take schedule backup of your websites using crontab. You can search for crontab on Google. I take my website’s backup using crontab. If you need help with crontab, you can inform me. I’ll try to help.

      I didn’t test GDrive sync option. You can check the sync option and try.

      Thanks. πŸ™‚

  4. Hello! I hope all is well. Thank you for putting this together. Having an update to date article that works is much needed. I’m curious if you’ve ever encountered an issue with large file uploads? I’m trying to upload a ~900mb file and within 30 seconds it seems to fail with an error I’ve been unable to debug. There is no repeating event such as reaching a certain amount uploaded or a certain amount of time – I’ve seen it fail at 10 seconds and 25, everything in between, 100mb and just under 900mb. I’ve added the –timeout 0 flag to try and resolve the issue and no dice. I’m kind of at a loss? Just hoping you might have some thoughts.

    Here are the errors produced:

    Thanks again!

  5. Maan, thanks a lot !
    I managed to do it and it works great.

    One little confusion I had was why we need the `go get` command. We have already cloned the repo in order to modify the file. As far as I’ve seen, this has created another copy of the repo in ~/go/ (BTW, the link should be towards the gdrive-ord rather than prassmusen).

    So when you say:
    > Using command line, go to the root folder of the project
    it wasn’t clear for me if I was supposed to go in the ~/go/… folder or the one which I have cloned. Well, obviously it was the cloned and modified one, but still …

    In any case, it works. Thank you very much !

    1. Hello Cipri Tom,

      Glad to know that it works for you.

      I understand your confusion. At first, I’ve tried without go get To compile gdrive, we’ve to import their package. They have mentioned this on their doc. I’m also not clear about this.:( If I find, I’ll let you know.

      You supposed to go in the ~/go/… folder or the one which you have cloned. Yeah, you’re right. I mean the same thing. I’ve just removed the confusion line from the article.

      Thank you so much for informing me of these confusions. πŸ™‚

  6. Hi

    This worked for me when i had set up, and i moved to focus on different things.
    Missed to check whether its working continuously.
    Now i am getting the following error

    >gdrive list
    Failed to list files: Get oauth2: cannot fetch token: 400 Bad Request
    Response: {
    “error”: “invalid_grant”,
    “error_description”: “Bad Request”

    I am assuming auth token got expired, and need to get refresh token , but am not sure its really because of this issue.
    I stumbled upon few links to get refresh token , but since i am uncertain about the error can you please help me validate this error, and how to fix the same ?


  7. Thanks very much for your detailed and easy-to-follow documentation.

    What does the following command do?
    go get
    It looks like it downloads the project into ~/go/src
    Why do we have to do this if we have already downloaded and edited manuallly in Step 3?
    Also I see the exact file I compiled in ~/go/bin . Does the compiler automatically install things there?

    A more substantial question–when we authenticate the first time using gdrive, it doesn’t ask us which Drive we want to use (for example a Shared Drive). Is there any way to add this functionality? That’s what happens in rclone, but I need the link-sharing functionality from gdrive.

      1. i am using latest version

        i am seeing that error on command
        go get

        and also when i do go build after editing credentials for drive api.

          1. it was problem related to caching environment, i disabled CGO module by command and its worked without a error
            go env -w CGO_ENABLED=”0″

            for O auth now need to Select as Desktop (other option as shown in tutorial at github is not available now in console)

            there is small correction required

            sudo mv gdrive /usr/sbin/gdrive
            We need to set file permission. Run this command to set permission:

            in below command
            sudo chmod 700 /server-bin-folder/gdrive

            it is suppose to be sudo chmod 700 /usr/sbin/gdrive

            Thank You for your Tutorial

          2. Hi Chetan,

            Nice to hear that it works on your server. I had written server-bin-folder for all servers. I’ve just edited the article. Thanks for letting me know the issue. πŸ™‚

  8. hi Md Obydullah, thanks for this. It’s very worthy for me.

    do you have a manual on how to use the commands properly? I want to know how to use gdrive sync specifically.

  9. Could you help me to create a backup cron, when it will go up to the google cloud of the error.


    Uploading to Google Drive…
    : no such file or directoryitename-backup-09-02-2021.tgz
    Uploaded to Google Drive.


    # Md. Obydullah is here!
    # Full backup script

    # compress the directory
    echo “Compressing directory…”
    tar -zcvf “websitename-backup-$(date ‘+%d-%m-%Y’).tgz” /var/www/domain-path/public_html/
    echo “Directory compressed.”
    echo “”

    # upload to google drive
    echo “Uploading to Google Drive…”
    gdrive upload –parent 1stKf2fGTDK1PmmhguQa2M1EugAAfIA9a “websitename-backup-$(date ‘+%d-%m-%Y’).tgz”
    echo “Uploaded to Google Drive.”

    # remove the compressed file
    echo “Clearing temporary files…”
    sudo rm “websitename-backup-$(date ‘+%d-%m-%Y’).tgz”
    echo “Cleared temporary files.”
    echo “”

      1. Hi MD Obydullah,

        Thank you very much for the quick response, so I don’t need the code to make they work normally, the problem when I run sh in the shell it returns me with this error.
        Could you help me where the patch would be set?

        1. Hi, just set correct path of backup file “websitename-backup-$(date ‘+%d-%m-%Y’).tgz”.

          # upload to google drive
          echo “Uploading to Google Drive…”
          gdrive upload –parent 1stKf2fGTDK1PmmhguQa2M1EugAAfIA9a “websitename-backup-$(date ‘+%d-%m-%Y’).tgz”
          echo “Uploaded to Google Drive.”

          If you still have the issue, you can come to TeamViewer. I’ll take a look. My email [email protected]. Thanks.

Leave a Reply