Google Drive SDK-搜索文件夹中的文件

时间:2013-07-15 12:36:29

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

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

我使用的是这个文件:
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)UnAuthorized

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

  

{“error”:{“errors”:[{       “域名”:“全球”,       “理由”:“必需”,       “message”:“需要登录”,       “locationType”:“标题”,       “location”:“授权”}],“代码”:401,“消息”:“需要登录”}}

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

修改
我有这个包含身份验证的参数:

DriveService service  

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

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

但现在当我尝试拍摄特定物品时,我不确定如何将service

组合起来

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

您可以开始查看C# quickstart

快速入门允许您从控制台进行授权,然后向您展示如何在Google云端硬盘上插入文件。

.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

未经测试但基于我在生产环境中运行的代码严重,因此它应该可以运行

答案 2 :(得分: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;

希望获得帮助。

答案 3 :(得分:0)

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