Upload to a shared drive with Google Drive API V3

For some reason 404 parent folder not found error

It's a story when using a shared drive with Google Drive API V3, because there was little information despite being super addicted, so make a note! When I try to upload a file to a folder in the shared drive and move it by specifying the ID of the parent folder in meta.Parents, for some reason I get the error File not found [folderid] 404 ... ・ If you specify a folder ID in MyDrive that is not a shared drive as a trial, it will succeed, so there seems to be no problem with authentication and program flow.

Uploading a shared drive is a hassle

Suddenly, I had to set the following two properties. --meta object's DriveId property --CreateRequest object's SupportsAllDrives property

DriveId is also copied from the URL

DriveId, like the ID of the parent folder, is the last string of the URL when you open the root directory of the shared drive in your browser. There is also a property called TeamDriveId, but you can use DriveId instead of Deprecated.

Complete

The overall code including authentication is now like ↓. It's almost a sample copy of Quick Start. (The code is C # SDK, but other SDKs will be the same)

GoogleDriveUploader.cs


string[] Scopes = { DriveService.Scope.DriveFile }; //DriveFile to upload
string ApplicationName = "Appropriate application name";

UserCredential credential;

using (var stream = new FileStream("client_secret_~~.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read))
{
    string credPath = "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
    Console.WriteLine("Credential file saved to: " + credPath);
}

// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

var meta = new Google.Apis.Drive.v3.Data.File()
{
    Name = "File to create",
    MimeType = "application/octet-stream", //Let's set appropriately
    DriveId = "Shared drive ID(Like 0ABcDeJiHiJKLmn23)",
    Parents = new List<string> { "Parent folder ID(Like 1auYhd9J9j ~)" }
};

using (var stream = new System.IO.FileStream("Full path of the file to upload", System.IO.FileMode.Open))
{
    //New addition
    var request = service.Files.Create(meta, stream, meta.MimeType);
    request.Fields = "id, name";
    request.SupportsAllDrives = true;
    var ret = request.UploadAsync().Result;

    if (ret.Status == Google.Apis.Upload.UploadStatus.Failed)
    {
        throw ret.Exception;
    }
}

Recommended Posts

Upload to a shared drive with Google Drive API V3
Upload images to Google Drive with Python
Upload files to Google Drive with Lambda (Python)
How to upload to a shared drive using pydrive
Python: Extract file information from shared drive with Google Drive API
Until we created a mechanism to upload files shared in Slack to Google Drive without a server
How to search Google Drive with Google Colaboratory
Regularly upload files to Google Drive using the Google Drive API in Python
Upload JPG file using Google Drive API in Python
Create a tweet heatmap with the Google Maps API
Upload a file to Dropbox
Mount google drive with google-drive-ocamlfuse
Access Google Drive with Python
Google Drive Api Tips (Python)
Post a message to Google Hangouts Chat with a thread (Python)
Upload and delete files to Google Cloud Storages with django-storage
Play with YouTube Data API v3 using Google API Python Client
Rubyist tried to make a simple API with Python + bottle + MySQL
A story about adding a REST API to a daemon made with Python
I tried hitting the Google API with Ruby and Python-Make the database a Spreadsheet and manage it with Google Drive
Image upload & download to Azure Storage. With Python + requests + REST API
How to send a request to the DMM (FANZA) API with python
Try to create a Qiita article with REST API [Environmental preparation]
Introducing Google Map API with rails
Create a REST API to operate dynamodb with the Django REST Framework
How to create a serverless machine learning API with AWS Lambda
I tried to introduce a serverless chatbot linked with Rakuten API to Teams
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Try using Dropbox API v2 with Go
Send data to DRF API with Vue.js
Make a cascade classifier with google colaboratory
[IOS] Use a shared sheet with Pythonista3.
[V11 ~] A memorandum to put in Misskey
I tried to make a Web API
Get holidays with the Google Calendar API
I created a tool to correct GPS data with Mapbox Map Matching API (Mapbox Map Matching API)
How to drop Google Docs in one folder in a .txt file with python
Get a list of articles posted by users with Python 3 Qiita API v2
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to make a simple image recognition API with Fast API and Tensorflow
How to display Map using Google Map API (Android)
How to upload with Heroku, Flask, Python, Git (4)
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
Upload a large number of images to Wordpress
How to draw a 2-axis graph with pyplot
Set up a local server with Go-File upload-
I want to analyze songs with Spotify API 2
I tried "License OCR" with Google Vision API
Display Google Maps API with Rails and pin display
How to develop a cart app with Django
Run Google Analytics API (core v3) in python
Try to draw a life curve with python
I want to make a game with Python
Automatic voice transcription with Google Cloud Speech API
Try to make a "cryptanalysis" cipher with Python
I want to analyze songs with Spotify API 1
How to use the Google Cloud Translation API
Pretend to be a server with two PCs
Try slack OAuth authentication with flask (Slack API V2)