Reputation: 23
I must be stupid I think. Using OAuth
.
Have the following piece of code
// Authenticate with Google
using (MemoryStream stream =
new MemoryStream(GetSecrets()))
{
string credPath = "token.json";
this.userCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
Works fine and sets this.userCredential
as expected
Now I want to create a StorageClient
But StorageClient
cannot be created directly from UserCredential
.
The only way I can see is to create from the AccessToken
GoogleCredential googleCredential = GoogleCredential.FromAccessToken(this.userCredential.Token.AccessToken);
return StorageClient.Create(googleCredential);
Problem is this token expires after an hour.
However, in C# I cannot for the life of me find a way to pass in the refresh token (which I have in the UserCredential) and have it refresh itself as needed
It seems possible in Java
though, using the .Builder
Does anyone know if this is possible ?
Upvotes: 2
Views: 391
Reputation: 1502526
The simplest way of achieving this is to create the StorageService
yourself, and then pass that to the StorageClientImpl
constructor:
var service = new StorageService(new BaseClientService.Initializer
{
HttpClientInitializer = userCredential,
ApplicationName = StorageClientImpl.ApplicationName,
});
StorageClient client = new StorageClientImpl(service);
We might consider adding an overload of Create
that takes an IConfigurableHttpClientInitializer
instead of a GoogleCredential
, in the future.
Upvotes: 3