I'm trying to get Folders and Files from Google Drive, and there is some non-obvious stuff I can't understand.
First of all, I'm getting 37 items as a result - and that's wrong, because I never had 37 files on my Drive, so where all those files came from ?
Second, I'm not receiving file metadata like "Size" or "Extension" or "Parents" - all these properties are "null" for at least half of all items returned - can someone explain what means is "Parents" are "null"?
And the last thing, can I get files and folders step-by-step, like in OneDrive, e.g., get all files and folders from root of a drive, and later get children from selected folder (or download item if it's file)?
As far as I know, I have to get all files and folders and then build a tree to show it to user, as for me that's not good, because of multi-parenting stuff etc.
Here's code I use to get files and properties:
public UserCredential Authorize()
{
UserCredential credential = null;
using (var stream =
new FileStream("path_to_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, "path_to_save_creds.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
return credential;
}
public List<File> RetrieveAllFiles(DriveService service)
{
List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
request.Fields =
"nextPageToken, files(name, id, size, kind, parents, sharedWithMeTime, shared, sharingUser, fileExtension, viewedByMe, viewedByMeTime, trashed)";
request.Spaces = "drive";
do
{
try
{
FileList files = request.Execute();
result.AddRange(files.Files);
request.PageToken = files.NextPageToken;
}
catch (Exception e)
{
/*handle exception here*/
}
} while (!String.IsNullOrEmpty(request.PageToken));
return result;
}
Here's how do I call methods described above:
UserCredential credential = Authorize();
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
var googleFiles = RetrieveAllFiles(service);
At this point, I have 37 items, and such properties as "fileExtension", "viewedByMe", "parents" are null. Can someone explain what happens?