Reputation: 4249
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
Upvotes: 3
Views: 4593
Reputation: 101
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.
Upvotes: 2
Reputation: 10623
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
Upvotes: 0
Reputation: 662
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
Upvotes: 3
Reputation: 9213
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
Upvotes: 3