How to Upload Files on Google Drive Folder having Read/ Write full Access

292 Views Asked by At

I have created a Test folder on my Google Drive. I want to upload files in this folder using the google drive hard-coded URL. Anyone can access this folder and anyone can add and delete files because I have provided full access to this folder. But the system throws an error "the remote server returned an error (400) bad request" on the UploadFile request. Below is my code. Please help to resolve the issue. Thanks

private void Upload(string fileName) { var client = new WebClient();

        var uri = new Uri("https://drive.google.com/drive/folders/1yPkWZf03yhihjkejQYrhuS_SMxh9j8AP?usp=sharing");

        try
        {
            client.Encoding = Encoding.UTF8;
            client.UploadFile(uri, fileName);

            //client.Headers.Add("fileName", Path.GetFileName(fileName));
            //var data = File.ReadAllBytes(fileName);
            //client.UploadDataAsync(uri, data);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
1

There are 1 best solutions below

0
On

This will never work. C#'s WebClient's UploadDataAsync performs a standard HTTP POST to the specified Uri. Your Uri isn't the kind that accept HTTP POST. Instead it shows Google Drive's web app and let user manage files in the folder, whether it is adding files or removing files.

To add files to Google Drive folder programmatically, use Google Drive API. Refer to: https://developers.google.com/drive/api/guides/folder The guide shows how to create folder and upload files to specified folder.

This guide: https://developers.google.com/workspace/guides/get-started shows how to get started if you have never used any of Google's API.