Reputation: 417
I have set up a C# application that currently successfully contacts Google Drive, lists files, downloads files and reads their contents. So far great, but now I need to be able to modify files and I am finding that for some reason, this is just not happening. I believe I am using the correct scope when I am authenticating and have tested numerous combinations to no avail. I am continuing to receive the following error when it hits 'updateRequest.Execute()':
The code I have for authenticating is provided below:
string[] scopes = { DriveService.Scope.Drive };
UserCredential credentials;
using (var stream = new FileStream(RESOURCE_DIRECTORY + "\\google_credentials_v2.json", FileMode.Open, FileAccess.Read))
{
string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
credentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
return credentials;
The code I am using to modify the file is below:
public static void MoveFileToNewFolder(DriveService service, string folderName, string fileId, string previousParents = "")
{
FilesResource.UpdateRequest updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileId);
updateRequest.Fields = "id, parents";
updateRequest.AddParents = GetFolderIDFromName(service, folderName);
updateRequest.RemoveParents = previousParents;
updateRequest.Execute();
}
Important things to note:
I am unsure of whether I needed to set this up as a service account instead, but as far as I understand the scopes, I should be able to modify files. My requirement is to be able to download and modify ALL files and folders that are in the drive, regardless of their origin.
Thank you in advance!
Upvotes: 1
Views: 585
Reputation: 116868
Insufficient permissions
Means exactly that the user who you are currently logged in with has not granted your application permission to do what you are trying to do.
In order to use the Files.update method you must have requested access to one of the following scopes
You appear to be requesting DriveService.Scope.Drive
which means that you have probably changed the scopes since the user with authenticated. So your application is running off of the credentials that were granted when you first requested consent.
The credentials for this user are stored in this folder
string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
You can delete the credentials file for "user"
which is the name you have hard coded for your user. Or you can run the following command first which will cause the user to revoke the access to your application and then next time you run it it should request access again
credential.RevokeTokenAsync(CancellationToken.None);
There is a third option you should be able to call a force prompt on the request but for the life of me i cant remember that command. I will look around a bit see if i can find it and edit this if i find it.
Service accounts
I am unsure of whether I needed to set this up as a service account instead
Unless your application is designed to only access a single drive account and you will not be requesting access to a users drive account you should not be using a service account
Upvotes: 1