刷新Google Storage Client的令牌

时间:2019-03-07 09:19:02

标签: c# .net google-cloud-platform

我一定很愚蠢。使用OAuth

具有以下代码

// 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);
        }

工作正常,并按预期设置this.userCredential

现在我要创建一个StorageClient

但是无法直接从StorageClient创建UserCredential

我唯一看到的方法是从AccessToken

创建
GoogleCredential googleCredential = GoogleCredential.FromAccessToken(this.userCredential.Token.AccessToken);                                        
        return StorageClient.Create(googleCredential);

问题是该令牌在一小时后到期。

但是,在C#中,我终生无法找到一种方法来传递刷新令牌(我在UserCredential中拥有该令牌)并根据需要自行刷新

使用Java

似乎在.Builder中是可能的

有人知道这是否可能吗?

1 个答案:

答案 0 :(得分:3)

最简单的方法是自己创建StorageService,然后将其传递给StorageClientImpl构造函数:

var service = new StorageService(new BaseClientService.Initializer
{
    HttpClientInitializer = userCredential,
    ApplicationName = StorageClientImpl.ApplicationName,
});

StorageClient client = new StorageClientImpl(service);

我们将来可能会考虑添加Create而不是IConfigurableHttpClientInitializer的重载GoogleCredential