Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
Google Drive API using C# - Uploading

I am trying to use Google Drive API from an asp.net application to upload files.

Problem: The code works locally but when uploaded to server nothing happens (the page just keeps loading...no consent screen showing. Help appreaciated. "UploadedPdf" folder is writable and client_secrets.json exists in the folder.

NOTE: I have not installed anything on the server but just uploaded all the files included the Google API dll's into the bin folder of the server.

UserCredential credential;
        using (var filestream = new FileStream(Server.MapPath("UploadedPdf/client_secrets.json"),
            FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(filestream).Secrets,
                new[] { DriveService.Scope.Drive },
                "user",
                CancellationToken.None,
                new FileDataStore(Server.MapPath("UploadedPdf/"))).Result;
        }

        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
        body.Title = "My document";
        body.Description = "A test document";
        body.MimeType = "text/plain";

        byte[] byteArray = System.IO.File.ReadAllBytes(Server.MapPath("UploadedPdf/sample.txt"));
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
        request.Upload();

        Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

FileDatastore puts files by default in %appData%. Normally I would do something like. (lets assume I use "user" as well)

new FileDataStore("Console.Analytics.Auth.Store")  

Then when I call it I get a directory called

%AppData%\Roaming\Console.Analytics.Auth.Store

in that directory now lives a file called

Google.Apis.Auth.OAuth2.Responses.TokenResponse-user

I haven't tested what doing

new FileDataStore(Server.MapPath("UploadedPdf/"))

will do but my guess is you are going to get a directory named

%AppData%\Roaming\serverpat/uploadpdf/

Which I don't think is what you are after. If that is actually what you are trying to do that you have made that directory writeable? You might want to lookinto using LocalFileDataStore instead.

I am not sure if this is your problem or not.

Also remember you are hard coding "user" so technically your code is going to ask you to authenticate once for "user" after that it has the authentication in that file for "user" so there is no reason to ask for it again.

over 4 years ago · Santiago Trujillo Report

0

I think you are calling the AuthorizeAsync() wrong. Use the await keyword and remote the .Result. You'll have to mark the method as async in order to do this.

I would also suggest using the UploadAsync() method instead. You don't need to load the entire file into memory with ReadAllBytes() either. Just use File.OpenRead() and pass the FileStream instead of the MemoryStream.

Google has some example code: https://developers.google.com/api-client-library/dotnet/guide/media_upload

over 4 years ago · Santiago Trujillo Report

0

Your code suggests that you are using "Installed Application" workflow/credentials, which is supposed to work fine if you are running your ASP.NET application locally. However, when you move the same code to a web server, its no more an installed application. It enters the category of "Web Application".

You need to go to Google Developer Console->Credentials->Create New Client ID and generate a new set of credentials for your web application. Also you need to change the OAuth Flow as per this guide:

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-aspnet-mvc

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!