1
votes

I am building feature to upload files from my web application to my clients google drive. I don't want application to authenticate the user. Who ever is uploading any file from my file i want that to be uploaded to my client's google drive.

I am assuming that Service Account is the One which is used for this purpose. am I correct ?

When i upload the files using following code it gets uploaded to successfully but they are not visible under "My Drive". Then i added a permission which basically shares that file with my gmail account. and then it is visible under "Shared With Me" section.

I want those files to be available under "My Drive" section just like normal files.

Can any one point out what i am doing wrong here ?

 private DriveService GetServiceInstance()
{
    try
    {
        var certificate = new X509Certificate2(Server.MapPath("path to .p12 key"), "notasecret", X509KeyStorageFlags.Exportable);

        string[] scopes = new string[] {
            DriveService.Scope.Drive, 
            DriveService.Scope.DriveFile,
            DriveService.Scope.DriveAppdata,
            DriveService.Scope.DriveMetadata
    };

        var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer("Client Email")
        {
            Scopes = scopes
        }.FromCertificate(certificate));
        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

        return service;
    }
    catch (Exception ex)
    {

        throw;
    }
}

protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (fu.HasFile)
            {
                DriveService service = GetServiceInstance();
                Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
                body.Title = fu.FileName;
                body.MimeType = fu.PostedFile.ContentType;

                FilesResource.InsertMediaUpload request = service.Files.Insert(body, fu.PostedFile.InputStream, fu.PostedFile.ContentType);
                request.Upload();

// I want to eliminate this part. where i do not have to give permission. As i want these files to get stored on same google drive account for which i have generated this service account and client keys    





 Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
                    Google.Apis.Drive.v2.Data.Permission newPermission = new Google.Apis.Drive.v2.Data.Permission();
                    newPermission.Value = "my email address";
                    newPermission.Type = "user";
                    newPermission.Role = "writer";

                    Google.Apis.Drive.v2.PermissionsResource.InsertRequest insertRequest = service.Permissions.Insert(newPermission, file.Id);
                    insertRequest.SendNotificationEmails = false;
                    insertRequest.Execute();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
2

2 Answers

1
votes

Don't use a Service Account. Embed a refresh token for the target account in your server. See How do I authorise an app (web or installed) without user intervention? (canonical ?)

-2
votes

I have created a specific folder and shared the folder to my email address. Uploaded the document to that folder. This resolved my issue.