Vb.net 使用googledriveapi获取文件列表
我试图通过谷歌API返回我的谷歌硬盘中的文件列表。一切正常,只是它不断返回一长串google.api.drive.v2.data.file文件,而不是实际文件。我的代码可能有问题,但我不确定。谢谢你的帮助Vb.net 使用googledriveapi获取文件列表,vb.net,list,google-app-engine,google-drive-api,Vb.net,List,Google App Engine,Google Drive Api,我试图通过谷歌API返回我的谷歌硬盘中的文件列表。一切正常,只是它不断返回一长串google.api.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 DriveMo
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
请看以下文档:
您的函数返回一个Google.api.Drive.v2.Data.File的列表,这是绝对正确的,如果您需要每个文件的文件名,则需要获取OriginalFilename属性。返回一个响应主体,主体中包含项目,每个项目都是响应
要下载文件,只需获取下载的URL并将其作为流读取
类似这样的东西(我能找到的唯一例子是C#)
//
///下载文件并返回包含其内容的字符串。
///
///
///负责创建授权web请求的身份验证程序。
///
///驱动器文件实例。
///如果成功,则为文件内容,否则为空。
publicstaticsystem.IO.Stream下载文件(
IAAuthenticator(身份验证程序,文件){
如果(!String.IsNullOrEmpty(file.DownloadUrl)){
试一试{
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(
新的Uri(file.DownloadUrl));
authenticator.ApplyAuthenticationToRequest(请求);
HttpWebResponse=(HttpWebResponse)request.GetResponse();
if(response.StatusCode==HttpStatusCode.OK){
返回response.GetResponseStream();
}否则{
控制台写入线(
出现错误:“+response.StatusDescription”);
返回null;
}
}捕获(例外e){
Console.WriteLine(“发生错误:+e.Message”);
返回null;
}
}否则{
//该文件在驱动器上未存储任何内容。
返回null;
}
}
代码从
示例中的代码也是C#抱歉。要添加到DalmTo和David的答案中,您需要清楚“文件”是什么意思。通常,驱动器术语使用“文件”来指代元数据,如标题、父文件夹、日期修改等。它使用术语“媒体”或“内容”来指代文件的内容。因此,如果您希望下载内容,这是一个两阶段的过程。首先,在您执行操作时获取ID(尽管我建议使用fields=来限制您获取的元数据量)。然后,对于每个ID,分别使用downloadUrl或ExportLink(非Google和Google文件类型)下载内容。如果只是要列出的文件名,只需显示“title”属性即可
/// <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;
}
}