I need to get list of files from google drive with asp.net mvc4 project. Google Drive is used as a file storage for the site. My code:
const string email = "xxx";
string path = AppDomain.CurrentDomain.BaseDirectory + "file.p12";
var certificate = new X509Certificate2(path,"notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(email){
Scopes = new[] { DriveService.Scope.Drive }
}.FromCertificate(certificate));
var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName="hikes"
});
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
do
{
FileList files = request.Execute();
result.AddRange(files.Items);
request.PageToken = files.NextPageToken;
} while (!String.IsNullOrEmpty(request.PageToken));
Tried on two different accounts. The result is only one pdf file called "How to get started with Drive". How do I get access to the files of my disk?
You need to share at least one folder from your own Google drive accounts with the service's account email created in Google console (416132713247-aimfq7...hj37795pcpvo3@developer.gserviceaccount.com): Right click > Share with > fill email & permissions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With