Google-api-dotnet-client: Consider GoogleCredential.WithHttpClientFactory

Created on 8 Apr 2020  路  6Comments  路  Source: googleapis/google-api-dotnet-client

We've seen a few issues where users have wanted to set a proxy locally - and they can do so via an HttpClientFactory for the main service, but doing that for auth is harder.
For GoogleCredential, adding a CreateWithHttpClientFactory method would be in line with our CreateWithUser call. That wouldn't help for UserCredential, admittedly.
Do we also want to provide an out-of-the-box proxy-supporting HttpClientFactory to avoid each user doing their own thing?

I'm not at all certain we want to do any of this, but we should at least consider it.

p3 feature request

Most helpful comment

I'll look into this.

All 6 comments

I'll look into this.

This will be an extremely useful addition. It is quite simple to initialize GoogleCredential or a ServiceAccountCredential from a json file with the current API:

var gc = GoogleCredential.FromStream(fileStream);
var sac = ServiceAccountCredetial.FromServiceAccountData(fileStream);

However, initializing a credential instance with a proxy configuration (i.e. an HttpClientFactory) is extremely complicated. The only way seems to be parsing the json file to extract the ID and Private Key and then do something like this:

var serviceAccountEmail = "[email protected]";
var serviceAccountKey = "....";
var credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
               {
                   HttpClientFactory = new ProxySupportedHttpClientFactory()
               }.FromPrivateKey(serviceAccountKey));

A more convenient API to achieve this result would go a long way towards improving the experience for developers who have to deal with proxy servers.

Possible solutions:

  1. Provide factory methods on GoogleCredential that accept an HttpClientFactory. The Java API client follows a similar approach.
var gc = GoogleCredential.FromStream(fileStream, new ProxySupportedHttpClientFactory());
  1. Less preferred, but alternatively we can make it easier to use the existing ServiceAccountCredential API as follows.
// still better than having to parse json
var noProxy = ServiceAccountCredential.FromServiceAccountData(fileStream);
var init = new ServiceAccountCredential.Initializer(noProxy) // This constructor is internal in the current API
{
    HttpClientFactory = new ProxySupportedHttpClientFactory(),
};

var withProxy = new ServiceAccountCredential(init);

I will look at this next week.

@jskeet FYI I'm on this now, it seems like it should fix the Storage issue reported internally that you were looking at yesterday.

@amanda-tarafa: Ah, yes - I can definitely see it helping with that. Thanks!

Was this page helpful?
0 / 5 - 0 ratings