1

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;
            }
        }
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Sachin Trivedi
  • 2,033
  • 4
  • 28
  • 57

2 Answers2

1

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 ?)

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • any way we can fetch the token pro grammatically when it gets expired. ? – Sachin Trivedi Aug 12 '15 at 05:58
  • Yes. First be clear that you understand what a Refresh Token is and what an Access Token is. Refresh tokens don't normally expire. Think of it as being the OAuth equivalent of a username/password. Access Tokens expire after one hour, and can be programmatically refreshed using the Refresh Token. The linked answer explains how. – pinoyyid Aug 12 '15 at 07:34
-2

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