Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GoogleDrive SDK-搜索文件夹中的文件_C#_Url_Request_Google Drive Api - Fatal编程技术网

C# GoogleDrive SDK-搜索文件夹中的文件

C# GoogleDrive SDK-搜索文件夹中的文件,c#,url,request,google-drive-api,C#,Url,Request,Google Drive Api,我正在尝试获取特定文件夹下的所有项目 我正在使用此文档: 基于此,我写了以下内容: 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

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

我正在使用此文档:

基于此,我写了以下内容:

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,“消息”:“需要登录”}

我也用这个问题作为参考:

我看不出我做了什么不同

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

DriveService service  
之前,为了获取所有文件,我做了以下操作:

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

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

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

[1]


[2]

您可以开始查看

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

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

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

未经测试,但它基于我在生产环境中运行的代码进行了大量测试,因此它应该可以工作

如果您在从特定文件夹检索文件和文件夹时遇到问题,请遵循此链接,它将使用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;
string FolderId=“1lh-YnjfDFMuPVisoM-5p8rPeKkihtw9”;
//定义请求的参数。
fileResource.ListRequest ListRequest=DriveService.Files.List();
listRequest.PageSize=10;
listRequest.Q=“”+FolderId+”,在父级中,trashed=false”;
listRequest.Fields=“nextPageToken,files(*)”;
//列出文件。
IList files=listRequest.Execute()
.档案;

希望对您有所帮助。

谢谢!我只使用了所有代码中的Q部分,但它起了作用!:)为什么答案出现在VB中?@Noob它是VB.net,因为它几乎是我当时在生产中使用的代码的复制粘贴,而函数签名在VB.net中是相同的,所以我更喜欢用一些信息快速给出答案,而不是用C#编写所有代码,具体的项目是一个查询或类似的东西正确的?