如何使用谷歌驱动器API访问谷歌驱动器上手动添加的文件夹和文件

时间:2018-01-04 13:46:45

标签: google-drive-api

我在google驱动器上使用google drive API创建了一个文件夹,并且能够搜索API App创建的文件夹和文件。在此文件夹中,我在谷歌硬盘上手动创建了一个文件夹和文件。现在我无法使用driveService.Files.List()方法找到我手动创建的文件或文件夹。有任何方法可以通过google drive API手动创建文件或文件夹。文件搜索代码是

var request = service.Files.List();
request.Q = "mimeType='application/vnd.google-apps.folder' and trashed=false";
request.Spaces = "drive";
var result = request.Execute();
foreach (var file in result.Files)
{
    string fileid = file.Id;
    string filename = file.Name;
}

2 个答案:

答案 0 :(得分:0)

通过将“应用权限请求”范围从“DriveService.Scope.DriveFile”更改为“DriveService.Scope.Drive”来解决此问题

private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = new ClientSecrets
    {
        ClientId = Your_GoogleAPI_ClientId,
        ClientSecret = Your_GoogleAPI_ClientSecret
    },
    Scopes = new[] { DriveService.Scope.Drive },
    DataStore = new FileDataStore(Your_GoogleApi_Credential_File_Path)
});

答案 1 :(得分:0)

我会这样做:

 function findMyFolder() {
        var folders = DriveApp.searchFolders(
            'mimeType="application/vnd.google-apps.folder" and trashed=false');
        while (folders.hasNext()) {
            var folder = folders.next();
            Logger.log("Folder ID:  " + folder.getId() + "  Folder Name:  " + folder.getName());
        }
    }

文件和文件夹不同。您正在搜索文件夹的文件。我也改变了引号,但这只是个人偏好。