3

I am trying to get all the items underneath a specific folder.

I was using this documentation:
https://developers.google.com/drive/v2/reference/files/list

And based on it I wrote the following:

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();

And I get an error:

(401) UnAuthorized

Or when I copy-paste the request to the browser I get this:

{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }

I also used this question as a reference:
Getting a list of files by folder on Drive SDK
And I can't see what I've done differently

EDIT:
I have this parameter that contains the authentication:

DriveService service  

Before, in order to get ALL files I was doing this:

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

But now when I'm trying to take specific items, I'm not sure how to combine that service

Community
  • 1
  • 1
user990635
  • 3,979
  • 13
  • 45
  • 66

4 Answers4

3

You need to authenticate your requests with a valid access token. Go through OAuth 2.0 to learn how to retrieve an access token or use our Java client library that comes with OAuth 2.0 support [2].

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

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

Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
3

You could start looking at the C# quickstart.

The quick start let you authorize from the console and then show you how to insert a file on Google Drive.

After you import the .Net Library into the project you can write a code like this to get the files:

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

Not tested but it's heavily based on the code I run in a production environment, so it should work

smokybob
  • 663
  • 6
  • 13
  • Thanks! I only used the Q part from all that code, but it worked! :) – user990635 Jul 16 '13 at 04:52
  • Why is the answer in what appears to be VB? – Noob Jul 21 '14 at 18:19
  • @Noob it's VB.net because it's almost a copy paste of the code I used in production at the time and function signatures are the same in VB.Net so I preferred to give a quick answer with some information instead of writing all the code back in C# – smokybob Jul 22 '14 at 19:50
2

Using googleApis V3. This is sample code:

 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;

Hope this help.

Datusa
  • 101
  • 1
  • 3
0

If you have a problem in retrieving files and folder from a specific folder then follow this link it will help you in detail Get all files and folder from specific folder in Google Drive

Community
  • 1
  • 1
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81