Flutter: Google Drive: File list always returns me 0
Retrieving files using Service account:
The service account is different from your Google account. This means that the Google Drive is also different between Service account and your account. So when the file is retrieved using Service account, please share the files in your Google Drive with the Service account. By this, the files in your Google Drive can be retrieved by the Service account.
Scopes:
In your script, DriveFileScope
and SpreadsheetsScope
are used as the scopes. DriveFileScope
is https://www.googleapis.com/auth/drive.file
. The official document says this scope as follows.
View and manage Google Drive files and folders that you have opened or created with this app
By this, in your script, how about modifying DriveFileScope
as follows?
-
DriveReadonlyScope
(https://www.googleapis.com/auth/drive.readonly
) -
DriveScope
(https://www.googleapis.com/auth/drive
)
Note:
- I think that this scope can be also used for your situation.
-
DriveApi.driveMetadataReadOnlyScope
(https://www.googleapis.com/auth/drive.metadata.readonly
)
-
References:
Comments
-
saiy2k over 1 year
I want to retrieve list of files from a Google drive folder. Authentication happens through Service account. Here is my code to do the same:
final _credentials = new ServiceAccountCredentials.fromJson(r''' { "private_key_id": "b5-xxxx-17", "private_key": "-----BEGIN PRIVATE KEY-----\nMI-xxxxk=\n-----END PRIVATE KEY-----\n", "client_email": "[email protected]", "client_id": "100000000000", "type": "service_account" } '''); final _SCOPES = [SheetsApi.DriveFileScope, SheetsApi.SpreadsheetsScope]; clientViaServiceAccount(_credentials, _SCOPES).then((http_client) { DriveApi driveApi = DriveApi(http_client); driveApi.files.list().then((files) { print('kind: ' + files.kind); print('list: ' + files.files.length.toString()); });
My log looks like this:
just: drive#fileList list: 0
In Google Developers console, Google Drive API is enabled and service account it linked properly (as far as I can check).
But I also got another piece of code which writes some data to a spreadsheet, with hardcoded
sheetID
and that code is working fine.Any help on what I am doing wrong here?
-
Tanaike over 4 yearsYou are trying to retrieve the file list using Service account. The service account is different from your Google account. So the Google Drive is also different between Service account and your account. I think that for example, as a test case, when you share a file in your Google drive with the Service account, the file is retrieved by your script. If you have already known this and I misunderstood your situation, I apologize.
-
saiy2k over 4 yearsThanks for the understanding that access needs to be given to
Service account
also. Will read more about it. But I tried giving access to files and folders in my google drive with[email protected]
and run my flutter script. It didn't work as well. -
Tanaike over 4 yearsThank you for replying. When I saw your script again, I noticed that the scopes might be required to be modified for your situation. So please modify
DriveFileScope
toDriveReadonlyScope
orDriveScope
. And try it again. If this was not the result you want, I apologize. -
Glen over 4 yearsshould it not be print('list: ' + files.length.toString());
-
saiy2k over 4 years@Tanaike yes. With the scope change, it worked. Thanks for pointing it out. If you could write it as answer, I can mark it as right answer.
-
saiy2k over 4 years@Glen Nope. It was files.files.length.toString()
-
Tanaike over 4 years@saiy2k Thank you for replying. I'm glad your issue was resolved. And thank you for your proposal. I posted it as an answer, because also I thought that your question and solution are useful for other users who have the same issue. Could you please confirm it?
-