如何从ExecuteAsync()方法获取字段

时间:2017-03-31 14:30:07

标签: c# asynchronous google-drive-api async-await

我熟悉在Google云端硬盘中创建文件夹,但我正在尝试使用异步方法。但是,在这样做时,我不确定如何获取我明确添加到我想要返回的字段中的字段。

我的代码如下:

private Task<Google.Apis.Drive.v3.Data.File> CreateGoogleDriveFolderAsync(DriveService service, string foldername, string parent_id = null)
{
    IList<string> parent_ids = new List<string>();

    Google.Apis.Drive.v3.Data.File folder = new Google.Apis.Drive.v3.Data.File
    {
        Name = foldername
        , MimeType = "application/vnd.google-apps.folder"
        , Description = "Client Name: blah\nUser: Rudy\n"
    };

    var insert = service.Files.Create(folder);

    // The field I'd like to get somewhere.
    insert.Fields = "id";

    var task = insert.ExecuteAsync();

    task.ContinueWith(t =>
    {
        // NotOnRanToCompletion - this code will be called if the upload fails
        Console.WriteLine("Failed to create folder \"{0}\": " + t.Exception, foldername);
    }, TaskContinuationOptions.NotOnRanToCompletion);
    task.ContinueWith(t =>
    {
        // I'd like a way to access "id" from my insert execution.
        log.insertLogging(foldername, "Directory Created");
    });

    return task;
}

1 个答案:

答案 0 :(得分:4)

使用documentationawait任务中的示例并从那里获取文件,您应该可以访问其属性

private async Task<File> CreateGoogleDriveFolderAsync(DriveService driveService, string foldername) {
    var metadata = new File()
    {
        Name = foldername,
        MimeType = "application/vnd.google-apps.folder"
    };
    var request = driveService.Files.Create(metadata);
    request.Fields = "id";
    var folder = await request.ExecuteAsync();
    Console.WriteLine("Folder ID: " + folder.Id);

    return folder;
}