Home > Software design >  Add shared permission and get public link of the uploaded file using GoogleDriveApi v3 in C# .Net?
Add shared permission and get public link of the uploaded file using GoogleDriveApi v3 in C# .Net?

Time:10-03

I am trying to get public shareable link of the uploaded files to GoogleDrive using GoogleDriveApi v3. I am able to upload the files and get FileId. I am not able to add Permission and shareable link to the uploaded files. I have searched numerous answers on the Internet and StackOverflow, but did not anything that would help me solve the problem. I have tried solutions that were provided for Java and Python, but I am getting error:

Google.GoogleApiException: 'The service drive has thrown an exception. HttpStatusCode is Forbidden. The resource body includes fields which are not directly writable.'

Here's my code:

public async Task UploadFileAsync(Stream file, string fileName, string fileMime, string folder, string fileDescription)
{
    DriveService driveService = GetService();
    var fileMetaData = new Google.Apis.Drive.v3.Data.File()
    {
        Name = filename, Description = fileDescription, MimeType = fileMime, Parents = new string[] { folder },
    };

    var request = driveService.Files.Create(fileMetaData, file, fileMime);
    request.Fields = "id, webContentLink";

    var response = await request.UploadAsync(cancellationToken);

    if (response.Status != UploadStatus.Completed)
        throw response. Exception;

    var permission = new Permission { AllowFileDiscovery = true, Id = "id, webContentLink", Type = "anyone", Role = "reader" };
    var createRequest = driveService.Permissions.Create(permission, request.ResponseBody.Id);
    createRequest.Fields = "id, webContentLink";
    await createRequest.ExecuteAsync();

    Debug.WriteLine("Link: "   request.ResponseBody.WebContentLink);
}

I am getting the link in the statement request.ResponseBody.WebContentLink, but the permissions are not set on the file. Hence the file is not shared and the link does not work. Is there anything I am doing wrong?

CodePudding user response:

Got it working after correcting the Permission initialization. Seems that Permission.Id is not writeable (hence the error The resource body includes fields which are not directly writable.). Thus, removed assignment of some value to Permission.Id.

Hence the correct way would be

var permission = new Permission { AllowFileDiscovery = true, Type = "anyone", Role = "reader" };
var fileId = request.ResponseBody.Id;
await driveService.Permissions.Create(permission, fileId).ExecuteAsync();

Hence, in this way we can set permissions on the uploaded file and make it shareable through code. Rest of the code is correct. Further we will get downloadable link using request.ResponseBody.WebContentLink.

CodePudding user response:

The sharable link created through the google drive web application is not something that you can create or get via the google drive API.

The WebView link at the download links can only be used by someone who has permissions on the file

so you will need to add the user as reader for the file if you want them to be able to use that link

API flow to upload a file and grant access to a user.

Create a file via the file.create method uploading the file.

Then preform a permissions.create adding the user who you would like to be able to have access to download the file.

Then do a file.get to get the WebContentLink and the user should be able to access the file via their google drive account.

  • Related