3

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

我正在使用此文档:
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) 未经授权

或者当我将请求复制粘贴到浏览器时,我得到以下信息:

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

我也用这个问题作为参考:
Getting a list of files by folder on Drive SDK
And I can't see what I've done different

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

DriveService service  

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

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

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

4

4 回答 4

3

您需要使用有效的访问令牌来验证您的请求。通过 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

于 2013-07-15T14:14:22.510 回答
3

您可以开始查看C# quickstart

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

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

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

未经测试,但它很大程度上基于我在生产环境中运行的代码,所以它应该可以工作

于 2013-07-15T21:06:35.343 回答
1

使用 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;

希望这有帮助。

于 2020-03-13T08:00:12.880 回答
0

如果您在从特定文件夹中检索文件和文件夹时遇到问题,请点击此链接,它将帮助您详细了解 Google Drive 中特定文件夹中的所有文件和文件夹

于 2014-04-03T11:18:38.993 回答