将自定义 HTTP 客户端与 Google 的 API go 库一起使用?

Use custom HTTP client with Google's API go library?

为了使用 Google 的 API go SDK,我们需要使用 token source. This works great on its own, but becomes a problem when using a custom HTTP client.

文档确实提到在使用自定义 HTTP 客户端时不会保留选项。这对我们来说是必要的,以便为我们的客户提供仪器。

有没有办法同时使用 HTTP 客户端和令牌源?

因为 WithHTTPClient 排除了任何其他选项的使用,备选方案是使用令牌源准备 http 客户端。为此,需要定义传输。

service, err := ggoauth2.NewService(
    ctx,
    option.WithHTTPClient(&http.Client{
        Timeout: 30 * time.Second,
        Transport: &oauth2.Transport{
            Base:   http.DefaultTransport,
            Source: tokenSource,
        },
    }),
)