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 github.com/prasmussen/gdrive

The gdrive binary should now be available at $GOPATH/src/github.com/prasmussen/gdrive

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**.apps.googleusercontent.com"
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:

https://accounts.google.com/o/oauth2/auth?client_id=123456789123-7n0vf****

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 test.zip

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

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/backup.sh >/dev/null 2>&1
Done…! I hope this article will help you. πŸ™‚

Author

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

Follow

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

  1. Great article, well written and timely help!
    Amazed to see how it helps

    Great job and keep it up!
    Such articles are really useful when we are stuck

  2. 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…

  3. 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.

  4. 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/backup.sh. 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. πŸ™‚

  5. 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:
    https://pastebin.com/L4DkgCAK

    Thanks again!

  6. 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 github.com/prasmussen/gdrive` 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/github.com/prasmussen. (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 github.com/prasmussen/gdrive. 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. πŸ™‚

      1. Yes first like that. but if I build it from source code gdrive ask for authentication again. can you give me you own gdrive file?

        1. Hi Aji,
          1. I’m sorry for the first reply. If you build source code, gdrive will ask for authentication again.
          2. For security reasons, I’m unable to share my gdrive file. πŸ™

  7. 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 https://www.googleapis.com/drive/v3/files?alt=json&fields=nextPageToken%2Cfiles%28id%2Cname%2Cmd5Checksum%2CmimeType%2Csize%2CcreatedTime%2Cparents%29&orderBy=&pageSize=30&q=trashed+%3D+false+and+%27me%27+in+owners: 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 http://www.howtosolvenow.com/how-to-get-refresh-token-for-google-drive-api/ 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 ?

    Thanks
    Madhumitha

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

    What does the following command do?
    go get github.com/prasmussen/gdrive
    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.

  9. i am getting error on
    # go build
    # runtime/cgo
    ccache: invalid option — ‘E’

    what i should do

      1. i am using latest version
        go1.14.3.linux-amd64.tar.gz

        i am seeing that error on command
        go get github.com/prasmussen/gdrive

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

        1. I’ve just tested again. I didn’t face any problem. Unfortunately, I didn’t understand your issue. You can uninstall Go and install again in new path. Then try. I think then it may work.

          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. πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *