【问题标题】:GoogleDrive SDK- searching files inside a folderGoogle Drive SDK-在文件夹中搜索文件
【发布时间】:2013-07-13 07:53:16
【问题描述】:

我正在尝试获取特定文件夹下的所有项目。

我正在使用这个文档:
https://developers.google.com/drive/v2/reference/files/list

在此基础上,我写了以下内容:

string url = string.Format("https://www.googleapis.com/drive/v2/files?'q'=\"'{0}' in parents\"", folderId);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
WebResponse response = request.GetResponse();

我得到一个错误:

(401) 未经授权

或者当我将请求复制粘贴到浏览器时,我会得到:

{“错误”:{“错误”:[{ “域”:“全球”, “原因”:“需要”, "message": "需要登录", “位置类型”:“标题”, “位置”:“授权”}],“代码”:401,“消息”:“需要登录”} }

我也用这个问题作为参考:
Getting a list of files by folder on Drive SDK
而且我看不出我做了什么不同的事情

编辑:
我有这个包含身份验证的参数:

DriveService service  

之前,为了获取所有文件,我这样做了:

FilesResource.ListRequest request = service.Files.List();   

但是现在当我尝试获取特定项目时,我不确定如何组合 service

【问题讨论】:

  • 通过特定项目是查询或类似的东西吗?

标签: c# url request google-drive-api


【解决方案1】:

您需要使用有效的访问令牌来验证您的请求。通过 OAuth 2.0 了解如何检索访问令牌或使用我们附带 OAuth 2.0 支持的 Java 客户端库 [2]。

[1]https://developers.google.com/accounts/docs/OAuth2

[2]https://developers.google.com/drive/auth/web-server

【讨论】:

    【解决方案2】:

    您可以开始查看C# quickstart

    快速入门让您可以从控制台授权,然后向您展示如何在 Google Drive 上插入文件。

    .Net Library 导入项目后,您可以编写如下代码来获取文件:

    Private Sub GetFileList(ParentFolder As String)
    
        Authorize() 'Take care of authorization... you could use JS to ask the user and get the Auth Token
    
        'MSO - 20130423 - Search the Google Drive File with the specified foldername
        'Create the search request
        Dim oListReq As Google.Apis.Drive.v2.FilesResource.ListRequest
        Dim oFileList As FileList
        'mimeType = 'application/vnd.google-apps.folder'
    
        oListReq = oDriveService.Files.List()
        'Search for a specific file name
        oListReq.Q = "mimeType = 'application/vnd.google-apps.folder' and title = '" + ParentFolder + "' and trashed=false"
        oListReq.Fields = "items/id" 'MSO - 20130621 - only ID needed for next query
        oListReq.MaxResults = 10 'Max 10 files (too may I Expect only 1)
    
        'Get the results
        oFileList = oListReq.Fetch()
    
        'Only 1 result is expected
        If oFileList.Items.Count = 1 Then
            Dim oFile As File = oFileList.Items(0)
            FolderId = oFile.Id 'Get FolderId
        End If
    
        oListReq = oDriveService.Files.List()
        'Search for a specific file name in the folder
        oListReq.Q = "'" + FolderId + "' in parents and trashed=false "
        'oListReq.Fields = "items(id,alternateLink)" 'MSO - 20130621 - Optimize your query if you need only certain fields
    
        'Get the results
        oFileList = oListReq.Fetch()
    
        'TODO: oFileList now have the list of the files in the folder, but there could me more "pages"
    
    End Sub
    

    未经测试,但根据我在生产环境中运行的代码,它大量,所以它应该可以工作

    【讨论】:

    • 谢谢!我只使用了所有代码中的 Q 部分,但它有效! :)
    • 为什么答案似乎是 VB?
    • @Noob 它是 VB.net,因为它几乎是我当时在生产中使用的代码的复制粘贴,并且函数签名在 VB.Net 中是相同的,所以我更愿意用一些快速回答信息而不是用 C# 写回所有代码
    【解决方案3】:

    如果您在从特定文件夹中检索文件和文件夹时遇到问题,请点击此链接,它将为您提供详细帮助 Get all files and folder from specific folder in Google Drive

    【讨论】:

      【解决方案4】:

      使用 googleApis V3。这是示例代码:

       string FolderId = "1lh-YnjfDFMuPVisoM-5p8rPeKkihtw9";
              // Define parameters of request.
              FilesResource.ListRequest listRequest = DriveService.Files.List();
              listRequest.PageSize = 10;
              listRequest.Q = "'" + FolderId + "' in parents and trashed=false";
              listRequest.Fields = "nextPageToken, files(*)";
      
              // List files.
              IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                  .Files;
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-07
        • 2017-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多