Apply for google drive api and use rclone to mount the team disk as a local disk

The team disk space of Google Cloud Disk is relatively large, and it is more convenient to mount it locally as a temporary storage or for caching. This article uses Rclone to mount it on a Linux server.

Rclone has a built-in API for accessing google drive , but if too many people share it, the speed limit may result in inaccessibility, so consider applying for your own API.

Table of contents

1 Apply for google drive api

2 mount google drive on the server

3 possible problems

3.1 Access prohibited during authorization

3.2 Fatal error: failed to get token: Post "https://oauth2.googleapis.com/token"


1 Apply for google drive api

First go to the google cloud console and create a project.

create a new project
create a new project
Fill in the project name as required

 After the creation is complete, select the project and enter the api configuration interface.

Enter the api configuration page

 Click "Enable APIs and Services"

Click "Start API and Services"

 Then, we search for "google drive" in the API library, or select Google drive in the libraries listed below

search google drive api

 After enabling the api, the webpage will automatically jump to the google cloud api console. At this time, we click "credentials" - "CREATE CREDENTIALS" - "OAuth client ID" in turn

Create an OAuth ID

According to the prompt, to configure OAuth ID, you first need to configure the consent screen.

 When configuring the consent screen, we choose "external", otherwise this api can only be used within the same organization.

Select "External"

 Then fill out the form marked with an asterisk. There may be an error "Error saving your application" here, most likely due to application name conflicts, you can try to use an unusual application name.

fill in the necessary information

 In the "Range" setting interface, we don't need to make any changes.

In the "Test User" interface, we can add our Google account to the test user.

The application we created has two states of "test" and "release". In the release state, everyone can access the application, and in the test state, only test users can access the application. In the future, we can freely choose whether to publish application.

Publish the app to be available to everyone, otherwise it can only be used by test users

 Now that we have the consent screen configured, move on to configuring the OAuth ID

Proceed to create OAuth ID

 Record the ID and password, or download the json file directly, which contains the ID and secret

Make a note of the ID and key

 So far, we have applied for the API of google drive, including an ID and a key.

 2 mount google drive on the server

First, you need to install Rclone on the server. Check the installation method of your system in the official rclone documentation . The following uses ubuntu20 as an example. Enter the following commands in sequence at the command line:

apt update
apt install curl
apt install fuse
curl https://rclone.org/install.sh | sudo bash

 After the installation is successful, enter rclone config on the command line, and enter "n" to create a new cloud disk, and then give it a name.

Create a new remote and name it

 Find google drive and enter the serial number 18 in front of it. Note that as the rclone version changes, the serial number of google drive may change, please confirm the serial number yourself.

Find Google Drive and enter the serial number

 After selecting Google Drive, we enter the api ID and secret we just obtained

Enter ID and key

 Then give the program full permissions

Choose Full access

 Next, service_account_file can be left blank, select No for advanced configuration and automatic configuration

service_account_file press Enter directly, select "n" for advanced and auto

 Then, we need to authenticate the application on the web. But because there is no browser in the server, we can only download the Windows version of rclone, run the command given by the program on Windows, and open the web page for authentication.

We first download the Windows version of rclone from the rclone download page , unzip the program to any path, and then press win+R to run cmd.

run cmd

 After entering cmd, use the cd command to enter the directory where the rclone program is located, and enter the command given in the linux system.

Copy the command in the red box

cd command to enter the rclone directory, and paste the command in to run

 After executing the command, the browser will automatically pop up the google account login interface, and log in with your google account. Since our application has not been officially certified by Google, the following prompts may appear, which is normal, just go to your application.

It is normal for this prompt to appear

 Next, authorize the app

authorized application

 If the authorization is successful, a Success message should appear on the web page.

success prompt

 Next, copy the obtained token and paste it into the config_token on the server side.

Fill in the obtained token back to the server

Here we want to mount the team disk, so choose y. If you want to mount your personal disk, fill in n.

Fill in "y" to mount the team disk

 Next, the program will list all the team disks under your account, just select the one you want to mount.

Select the team disk to mount

 Next, confirm the information:

confirm information

 At this point, we can see that a cloud disk named "googledisk" has been created, just press "q" to exit the program.

The cloud disk has been created

 Next, we need to mount the newly created cloud disk to the system.

The command to mount the network disk is:

rclone mount 网盘名:网盘下的目录 即将挂载到的目录 --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

Network disk name: the name of the network disk we just created in rclone, which is googledisk

The directory under the network disk: the directory under the Google network disk

The directory to be mounted to: the path in the server system

Take myself as an example:

 After executing the command, if there are no other errors, the program will be stuck here. We don't want to end the command, but directly disconnect the terminal, let it continue to run, and open a new terminal for subsequent operations.

Use the df -h command to check the system disk status, and you can see that the cloud disk has been successfully mounted.

Successfully mounted the created network disk

 At this point, we can access the files in the network disk normally!

Normal access to network disk files

 The above is the method of mounting Google cloud disk, and the mounting methods of other cloud disks are similar.

3 possible problems

3.1 Access prohibited during authorization

It may be caused by the fact that the account does not have access rights to the application. You can check whether the application is published. If the application is in the test state, you can add your own account to the test user.

3.2 Fatal error: failed to get token: Post "https://oauth2.googleapis.com/token"

The specific error is shown in the figure. Although the program displays Success on the web page, rclone cannot obtain the token.

rclone can't get the token

报错:A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

If such an error occurs, it proves that cmd is not connected to Google's network. At this time, an appropriate proxy should be set in cmd. For details, please refer to: Setting the proxy of the cmd command line terminal of the Windows system .

Guess you like

Origin blog.csdn.net/diqiudq/article/details/126070602