I am trying to upload a file onto my Drive using Google Drive .NET API v3. My code is below
static string[] Scopes = { DriveService.Scope.Drive,
Have a look at what request.Upload()
returns. For me when I was having this issue it returned:
Insufficient Permission Errors [Message[Insufficient Permission] Location[ - ]
I changed my scope from DriveService.Scope.DriveReadonly
to DriveService.Scope.Drive
and I was in business.
Try to visit this post from ASP.NET forum.
The same idea as what you want to do in your app, since you are dealing with uploading a file in Google Drive using .net.
You may try to call rest api directly to achieve your requirement :
The quickstart from .net will help you to make requests from/to the Drive API.
Upload Files:
The Drive API allows you to upload file data when create or updating a File resource.
You can send upload requests in any of the following ways:
- Simple upload:
uploadType=media
. For quick transfer of a small file (5 MB or less). To perform a simple upload, refer to Performing a Simple Upload.- Multipart upload:
uploadType=multipart
. For quick transfer of a small file (5 MB or less) and metadata describing the file, all in a single request. To perform a multipart upload, refer to Performing a Multipart Upload.- Resumable upload:
uploadType=resumable
. For more reliable transfer, especially important with large files. Resumable uploads are a good choice for most applications, since they also work for small files at the cost of one additional HTTP request per upload. To perform a resumable upload, refer to Performing a Resumable Upload.
You may try this code from the documentation on uploading sample file.
var fileMetadata = new File()
{
Name = "photo.jpg"
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("files/photo.jpg",
System.IO.FileMode.Open))
{
request = driveService.Files.Create(
fileMetadata, stream, "image/jpeg");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);
You may check the errors you may encounter in this documentation.
string[] Scopes = { DriveService.Scope.Drive };
Change static string[] Scopes = { DriveService.Scope.DriveReadonly };
to static string[] Scopes = { DriveService.Scope.Drive };
.
After changes, take a look into token.json
file and check does it change its scope from DriveReadonly
to Drive
.
If you are seeing DriveReadonly
then delete the token.json
file and run the application again.