如何使用resourcekey Google Drive API获取文件?

How to get file with resourcekey Google Drive API?

我在这里找到


https://developers.google.com/drive/api/v3/resource-keys

我确实得到了 folder/file 的 resourcekey。但是如何使用resourcekey获取复制文件或文件夹呢? 我用代码

DriveService.Files.Get(id).Execute()

它以前更新过。但是现在不要了。我搜索了很多 post 但没有解决。对不起我的英语不好。感谢阅读。 编辑:我使用 C#。

您需要在请求中添加X-Goog-Drive-Resource-Keys header。在客户端库中执行此操作的最简单方法是通过请求拦截器。它有点笨拙,但实际上并不复杂:

public class HeaderExecuteInterceptor : IHttpExecuteInterceptor
{
    private readonly string header;
    private readonly string value;

    public HeaderExecuteInterceptor(string header, string value)
    {
        this.header = header;
        this.value = value;
    }

    public Task InterceptAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        request.Headers.Add(header, value);
        // The value doesn't matter; Task.CompletedTask is simpler where supported.
        return Task.FromResult(true);
    }
}


// Where you make the request
const string ResourceKeysHeader = "X-Goog-Drive-Resource-Keys";
var request = service.Files.Get(id);
var interceptor = new HeaderExecuteInterceptor(ResourceKeysHeader, resourceKey);
request.AddExecuteInterceptor(interceptor);
var response = request.Execute();

或者,任何人都可以使用此代码。

DriveService.HttpClient.DefaultRequestHeaders.Add("X-Goog-Drive-Resource-Keys", "ID/ResourceKey")
DriveService.Files.Get(id).Execute()

“/”为必填项。