Upload to Google Drive without any UI?

后端 未结 4 481
北荒
北荒 2021-02-03 21:57

I am looking to write a server application to upload files to Google Drive. I was previously using the Documents List API, but I see that is deprecated. I\'d like to move to the

4条回答
  •  孤城傲影
    2021-02-03 22:27

    Since the gdrive tool is no longer maintained. I found an official and better way.

    Using curl.

    1. Install Curl

    sudo apt install curl

    1. Create your project credentials on google console. Make an account first if you don't have any.

    Create credentials > Configure OAth consent screen (if needed) > Application type > TV and limited input devices > Save your client id and a client secret.

    1. Verify the device

    curl -d "client_id=&scope=https://www.googleapis.com/auth/drive.file" https://oauth2.googleapis.com/device/code

    Expected response:

    {"device_code": "",
    "user_code": "xxx-xxx-xxx",
    "expires_in": 1800,
    "interval": 5,
    "verification_url": "https://www.google.com/device"}
    

    Then go to the https://www.google.com/device --> Enter "user_code" --> Give relevant permissions.

    1. Save the "device_code" and "user_code" values.
    2. Get Bearer code

    curl -d client_id= -d client_secret= -d device_code= -d grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code https://accounts.google.com/o/oauth2/token

    Expected Output:

    {
    "access_token": ".....",
    "expires_in": 3599,
    "refresh_token": "....",
    "scope": "https://www.googleapis.com/auth/drive.file",
    "token_type": "Bearer"
    }
    
    1. Save the "access_token" value.

    2. Start uploading

    curl -X POST -L -H "Authorization: Bearer " -F "metadata={name :'filename.zip'};type=application/json;charset=UTF-8" -F "file=@filename.zip;type=application/zip" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

    CAUTION

    Make zip archives of your files before uploading. The above code works for archived files. I uploaded 10gb archive with the above method successfully.

    Source

提交回复
热议问题