我正在尝试通过Google API返回我的Google云端硬盘中的文件列表。一切正常,但它不断返回 google.apis.drive.v2.data.file 的长列表而不是实际的文件。我的代码可能有问题,但我不确定。谢谢你的帮助!
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)
我用它来调用这段代码。
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
答案 0 :(得分:1)
查看文档:{{3}}
您的函数会返回一个Google.Apis.Drive.v2.Data.File列表,如果您需要获取 OriginalFilename 属性所需的文件名,这绝对没问题。
答案 1 :(得分:0)
service.Files.List返回一个响应主体,在该主体中的每个项目都是file resource。
要下载文件,您只需要获取downloadURL并将其作为流读取。
像这样的东西(只有我能找到的例子是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;
}
}
代码摘自Files:get
示例lib中的代码也是C#抱歉。
答案 2 :(得分:0)
要添加DalmTo和David的答案,你需要明确你的意思&#34; file&#34;。通常,Drive命名法使用&#34; File&#34;引用标题,父文件夹,dateModified等元数据。它使用术语&#34; media&#34;或&#34;内容&#34;引用文件的内容。因此,如果您希望下载内容,则需要两个阶段。首先获取你正在做的ID(尽管我建议使用fields =来限制你获取的元数据量)。然后,对于每个ID,分别使用downloadUrl下载内容,或分别为非Google和Google文件类型下载ExportLinks。如果它只是您要列出的文件名,只需显示&#34;标题&#34;属性。