-1
using Google.Apis.Auth.OAuth2;

using Google.Apis.Drive.v3;

using Google.Apis.Services;

using Google.Apis.Util.Store;

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;


namespace test
{
    internal class Program
    {
        static void Main(string[] args)
        {
        }

        public DriveService GetService()
        {
            ClientSecrets clientSecrets = new ClientSecrets
            {
                ClientId = "ClientId", // <- change
                ClientSecret = "ClientSecret" // <- change
            };

            /* set where to save access token. The token stores the user's access and refresh tokens, and is created automatically when the authorization flow completes for the first time. */
            string tokenPath = "Token_save_location"; // <- change

            string[] scopes = { DriveService.Scope.Drive };

            UserCredential authorizedUserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
              clientSecrets,
              scopes,
              "user",
              CancellationToken.None,
              new FileDataStore(tokenPath, true)
            ).Result;

            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = authorizedUserCredential,
                ApplicationName = "Name_of_your_application", // <- change
            });
            return service;
        }

        public DriveService GetClient()
        {
            string credenitalsJSONPath = $"private_key_path"; // <- change

            using (var stream = new FileStream(credenitalsJSONPath, FileMode.Open, FileAccess.Read))
            {
                var credentials = GoogleCredential.FromStream(stream);

                if (credentials != null)
                {
                    if (credentials.IsCreateScopedRequired)
                    {
                        string[] scopes = { DriveService.Scope.Drive };
                        credentials = credentials.CreateScoped(scopes);
                    }

                    var service = new DriveService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credentials,
                        ApplicationName = "Name_of_your_application", // <- change
                    });

                    return service;
                }
            }
            return null;
        }
    }
}

NOW HOW DO I LIST ALL THE FILES FROM MY GOOGLE DRIVE

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Welcome to stack please read [ask] it is best to explain what it is you are doing first then post your code and describe your issues. Just dumping your code here isnt going to be enough for most people to help you. – Linda Lawton - DaImTo Oct 24 '22 at 11:14

1 Answers1

0

You have a method called GetClient() which is returning your drive service object. This drive service object is what you use to make all your calls to the api.

Something like this should return you a list of files on your drive account.

var service = GetClient();
var results = service.Files.List().Execute();

Now if you want to only return a specific type of file you would use the search method.

var request = service.Files.List();   // Create file list request.
request.Q = "mimeType= 'video/mp4'";  // List only files of this mimetype
var results = request.Execute();

There are a number of diffrent mine types for videos you will need to check your drive account to see which ones you are looking for.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449