DEV Community

Ryan Esteves
Ryan Esteves

Posted on

Fixing Google BigQuery Auth Proxying

TL;DR

  • builder.Credential = credential; - Fails to route authentication traffic through the proxy.
  • builder.GoogleCredential = credential; - Successfully routes OAuth token requests through the proxy.

If your C# application runs behind a proxy, you have probably run into an annoying issue with the Google BigQuery client libraries: your BigQuery query calls route through the proxy perfectly by setting the HttpClientFactory property on the BigQueryClientBuilder, but the authentication and token refresh requests ignore the proxy entirely and attempt to hit the standard network, causing timeouts.

The Solution

To resolve this, set a proxied GoogleCredential to the GoogleCredential property instead of the Credential property on the client builder.

Example code:

using System.Net;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Http;
using Google.Cloud.BigQuery.V2;

// 1. Configure your proxy
IWebProxy proxySettings = new WebProxy("http://your-proxy-host:8080", bypassOnLocal: true)
{
    Credentials = new NetworkCredential("username", "password")
};

// 2. Wrap it inside Google's API HttpClientFactory
HttpClientFactory proxyClientFactory = HttpClientFactory.ForProxy(proxySettings);

// 3. Load credentials and create a new credential using the proxied http client factory 
GoogleCredential credential = await GoogleCredential.FromFileAsync("path/to/service-account.json");
credential = credential.CreateWithHttpClientFactory(proxyClientFactory);

// 4. Construct the client
BigQueryClient bigQueryClient = new BigQueryClientBuilder
{
    ProjectId = "your-google-cloud-project-id",
    HttpClientFactory = proxyClientFactory, // Proxies BigQuery data calls
    GoogleCredential = credential          // CRITICAL: Proxies Auth/token calls
}.Build();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)