1

I use the following code which is literally copied from Google's own .NET quickstart to list the files in your Google Drive account (the auth part has been adapted to use a refresh-token obtained by following the steps outlined here).

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using System;
using System.Diagnostics;

namespace GoogleTest {
    class Program {
        [STAThread]
        static void Main(string[] args) {
            var sw = Stopwatch.StartNew();
            var secrets = new ClientSecrets() {
                ClientId = "paste-your-client-id-here",
                ClientSecret = "paste-your-client-secret-here"
            };
            var token = new TokenResponse { RefreshToken = "paste-your-refresh-token-here" };
            var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
                new GoogleAuthorizationCodeFlow.Initializer {
                    ClientSecrets = secrets
                }),
                "user",
                token);

            Console.WriteLine($"1: {sw.Elapsed}");
            // Create the service.
            var service = new DriveService(new BaseClientService.Initializer() {
                HttpClientInitializer = credentials
            });
            Console.WriteLine($"2: {sw.Elapsed}");
            // Define parameters of request.
            var listRequest = service.Files.List();
            listRequest.PageSize = 10;
            listRequest.Fields = "nextPageToken, files(id, name)";

            Console.WriteLine($"3: {sw.Elapsed}");
            // List files.
            var files = listRequest.Execute().Files;
            Console.WriteLine($"4: {sw.Elapsed}");
            if (files != null && files.Count > 0) {
                foreach (var file in files) {
                    Console.WriteLine("{0} ({1})", file.Name, file.Id);
                }
            } else {
                Console.WriteLine("No files found.");
            }
            Console.WriteLine($"5: {sw.Elapsed}");
        }
    }
}

Running the above code produces output along the following lines:

1: 00:00:00.1075210
2: 00:00:00.1881154
3: 00:00:00.1987953
4: 00:00:12.5158776
No files found.
5: 00:00:12.5166778
Press any key to continue . . .

In other words, the var files = listRequest.Execute().Files call takes 10 seconds or more. Often it will even take up to 20 seconds.

Surely this cannot possibly be the normal response time for such a trivial request. Am I missing something here? As you can see, the Google Drive account in question does not even contain any files and is completely empty.

user3700562
  • 693
  • 11
  • 23
  • Check your internet connection, try pinging the server and seeing what response times you get? Sounds like a networking issue rather than programming. Do remember though, the Google Workspace APIs aren't the fastest thing in the world (though I do agree 10-20 seconds is *too* slow) - I mean, they're free to use right? – Rafa Guillermo Apr 05 '21 at 08:37
  • well my internet connection is fine from what I can tell. And I ran the test on two different machines...one server that hosts multiple websites and from my computer at home. It's no big deal, I just expected more from a google service I guess. – user3700562 Apr 06 '21 at 17:37
  • how many files do you have? You might only retrieve 10, but Drive scans all of your files before deciding which 10 to return. – pinoyyid Apr 07 '21 at 00:15
  • There are times when the API is very slow. For example, I generate PDFs and save the information into google sheets and then upload the PDF to gdrive. 90% of the time it is instant, but there are times when it takes 3 to 4 minutes for the page to finish doing what it is supposed to. The data and file are generated instantly but for some reason, the API takes a while to respond after entering the data and making the file. – Divern Mar 29 '22 at 06:05
  • I'm seeing the same thing on a much smaller scale. I'm doing a simple Files.list with q set to "'a folder ID' in parents". It takes around two seconds; my drive only contains about 13,000 files. The parent folder I'm listing only has about 250 files. Viewing a list of those files the the Drive web interface takes a fraction of a second. What's up with this API?? – Robert M. Jun 22 '23 at 08:18

1 Answers1

0

Response time from Google Apis depend upon a lot of variables.

  1. What other users are doing on the server at the same time your call comes in
  2. What time of day your call comes in will increase the chance there are others running their cron jobs as well.
  3. Your internet speed.

You need to remember this is a Free api you get what you pay for. Yes sometimes the calls return slow but your not paying for this.

prop tip dont run any script on the hour or on the half hour most people with cron jobs will set them to run on the hour and there for the servers will likely be more overloaded.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 2
    Yes I guess you are right, although I would think that Google of all companies surely would be in a position both technically and financially to guarantee better response times no matter the time of day or the number of users. Every other company is able to do it. Also I am a paying customer so it's not free in my case. – user3700562 Apr 06 '21 at 17:39
  • 3
    @user3700562 The correct reply to the admonition not to expect much from a free API is that Google should provide a paid one that works -- or fix the darn thing. No need to be high and mighty about it being free if they don't provide a paid one that works. – user2297550 Sep 07 '21 at 06:35
  • @user3700562 Indeed and...it's not free. I pay for Google Drive. – Robert M. Jun 22 '23 at 08:19
  • @RobertM. you pay for google drive you do not pay for access to the API. API access is free. – Linda Lawton - DaImTo Jun 22 '23 at 09:51
  • @LindaLawton-DaImTo I think the point is that there should be a non-sluggish version of the API for paying users. – Robert M. Jun 22 '23 at 21:25
  • I have heard a rumor that if have a huge app with a large quota Google may ask you to pay for using the API. In the mean time its a free api so no one pays for it. Google drive web application is not the same as Google Apis. Your not paying for it its free even if you pay for access to the web application. – Linda Lawton - DaImTo Jun 23 '23 at 09:28