我正在使用以下服务代码来检索用户的文件夹。 我启用了驱动器 api 并想使用服务帐户...
驱动器中有文件夹,但执行没有返回任何文件夹。
可能缺少什么?
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
namespace GoogleDriveService.Business.Services;
public class GoogleApiDriveService
{
private DriveService _driveService;
public GoogleApiDriveService()
{
var credentialsFilePath = "myservice-12aaae22210a.json";
GoogleCredential credential = GoogleCredential.FromFile(credentialsFilePath)
.CreateScoped([DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveReadonly]);
var initializer = new BaseClientService.Initializer { HttpClientInitializer = credential };
_driveService = new DriveService(initializer);
}
public List<Google.Apis.Drive.v3.Data.File> GetFolders()
{
var request = _driveService.Files.List();
request.IncludeItemsFromAllDrives = true;
request.SupportsAllDrives = true;
request.Q = "mimeType='application/vnd.google-apps.folder'";
var result = request.Execute();
return result.Files.ToList();
}
}
我启用了驱动器 API,下载服务帐户密钥,然后运行代码。
如果我打电话给 about 我会得到回复...
var req = _driveService.About.Get();
req.Fields = "*";
var about = req.Execute();`
about.get方法获取有关用户、用户的云端硬盘和系统功能的信息。
这不会为您列出文件。
为此,您需要使用 files.list 方法
// Create the Drive service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
var result = await service.Files.List().ExecuteAsync();
foreach (var file in result.Files)
{
Console.WriteLine(file.Id + " : " + file.Name + " - " + file.MimeType );
}