【问题标题】:Using Google Drive API to get list of files使用 Google Drive API 获取文件列表
【发布时间】:2015-04-14 13:38:41
【问题描述】:

我正在尝试通过 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

【问题讨论】:

    标签: vb.net list google-app-engine google-drive-api


    【解决方案1】:

    查看文档:Drive API

    您的函数返回一个绝对可以的 Google.Apis.Drive.v2.Data.File 列表,如果您需要每个文件名,您需要获取 OriginalFilename 属性。

    【讨论】:

      【解决方案2】:

      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# 抱歉。

      【讨论】:

        【解决方案3】:

        要添加到 DalmTo 和 David 的答案,您需要清楚“文件”的含义。通常,驱动器命名法使用“文件”来指代元数据,例如标题、父文件夹、日期修改等。它使用术语“媒体”或“内容”来指代文件的内容。因此,如果您希望下载内容,这是一个两阶段的过程。首先按照您的操作获取 ID(尽管我建议使用 fields= 来限制您获取的元数据量)。然后对于每个 ID,使用 downloadUrl 下载内容,或者分别使用非 Google 和 Google 文件类型的 exportLinks 下载内容。如果只是要列出的文件名,只需显示“title”属性即可。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多