StackUser
StackUser

Reputation: 145

How to get file with resourcekey Google Drive API?

I found here

https://stackoverflow.com/questions/68777408/how-to-obtain-a-resourcekey-using-google-drive-api

and

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

I did get resourcekey of folder/file. But how to get copy file or folder with resourcekey? I use code

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

It work update before. But now don't. I search many post but don't solve. Sorry my English not good. Thank for read. Edit: I use C#.

Upvotes: 1

Views: 612

Answers (2)

StackUser
StackUser

Reputation: 145

Alternatively, anyone can use this code.

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

The "/" is required.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500155

You need to add the X-Goog-Drive-Resource-Keys header in your request. The simplest way to do that in the client libraries is via request interceptors. It's a little clunky, but not actually complicated:

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();

Upvotes: 2

Related Questions