Reputation: 11
I am trying to Return the list of files in my Google drive through the Google API. Everything works fine except it keeps returning a long list of google.apis.drive.v2.data.file instead of the actual files. It's probably something wrong in my code but i am not sure. Thanks for the help!
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim bob As New GoogleDrive
Dim joe As New DriveModifyDate
Dim items As String = String.Join(Environment.NewLine, joe.GetFiles(bob.service, ""))
MsgBox(items)
I use that to call this piece of code.
Public Function GetFiles(ByVal service As DriveService, ByVal search As String) As IList(Of File)
Dim Files As IList(Of File) = New List(Of File)
Try
'List all of the files and directories for the current user.
Dim list As FilesResource.ListRequest = service.Files.List
list.MaxResults = 1000
If (Not (search) Is Nothing) Then
list.Q = search
End If
Dim filesFeed As FileList = list.Execute
'/ Loop through until we arrive at an empty page
While (Not (filesFeed.Items) Is Nothing)
' Adding each item to the list.
For Each item As File In filesFeed.Items
Files.Add(item)
Next
' We will know we are on the last page when the next page token is
' null.
' If this is the case, break.
If (filesFeed.NextPageToken Is Nothing) Then
Exit While
End If
' Prepare the next page of results
list.PageToken = filesFeed.NextPageToken
' Execute and process the next page request
filesFeed = list.Execute
End While
Catch ex As Exception
' In the event there is an error with the request.
MsgBox(ex.Message)
End Try
Return Files
End Function
Upvotes: 1
Views: 1381
Reputation: 22306
To add to DalmTo's and David's answers, you need to be clear what you mean by "file". Generally the Drive nomenclature uses "File" to refer to the metadata such as title, parent folder, dateModified, etc. It uses the term "media" or "content" to refer to the file's content. So if you wish to download the content, it's a two stage process. Firstly get the ID's as you are doing (although I would suggest using fields= to limit the volume of meta data you fetch). Then for each ID, download the content using the downloadUrl, or exportLinks for non-Google and Google file types respectively. If it's just the file names that you want to list, just display the "title" property.
Upvotes: 0
Reputation: 117176
service.Files.List returnes a response body, with in that body are items each item is a file resource.
To down load the file, you just need to take the downloadURL and read from it as a stream.
Something like this (Only example I can find is C#)
/// <summary>
/// Download a file and return a string with its content.
/// </summary>
/// <param name="authenticator">
/// Authenticator responsible for creating authorized web requests.
/// </param>
/// <param name="file">Drive File instance.</param>
/// <returns>File's content if successful, null otherwise.</returns>
public static System.IO.Stream DownloadFile(
IAuthenticator authenticator, File file) {
if (!String.IsNullOrEmpty(file.DownloadUrl)) {
try {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
new Uri(file.DownloadUrl));
authenticator.ApplyAuthenticationToRequest(request);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
return response.GetResponseStream();
} else {
Console.WriteLine(
"An error occurred: " + response.StatusDescription);
return null;
}
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
code ripped from Files:get
Code in the sample lib also C# sorry.
Upvotes: 0
Reputation: 2333
Take a look at the docs: Drive API
Your function returns a list of Google.Apis.Drive.v2.Data.File which is absolute ok, if you need the filename of each you need to get the OriginalFilename property.
Upvotes: 1