如何将自定义客户端传递给 Golang oauth2.Exchange
How to pass custom client to Golang oauth2.Exchange
oauth2.Exchange 在 https://godoc.org/golang.org/x/oauth2#Config.Exchange 的文档说:
The HTTP client to use is derived from the context. If a client is not provided via the context, http.DefaultClient is used.
现在我已经用自己的设置创建了一个 http.Client。我如何获得 oauth2.Exchange 函数来使用它?
oauth2
包定义 the following variable:
var HTTPClient internal.ContextKey
HTTPClient is the context key to use with golang.org/x/net/context's WithValue function to associate an *http.Client value with a context.
构造上下文时使用上述变量作为上下文键传递给Exchange
:
client := &http.Client{
// TODO
}
parent := oauth2.NoContext()
ctx := context.WithValue(parent, oauth2.HTTPClient, client)
tkn, err := c.Exchange(ctx, code)
oauth2.Exchange 在 https://godoc.org/golang.org/x/oauth2#Config.Exchange 的文档说:
The HTTP client to use is derived from the context. If a client is not provided via the context, http.DefaultClient is used.
现在我已经用自己的设置创建了一个 http.Client。我如何获得 oauth2.Exchange 函数来使用它?
oauth2
包定义 the following variable:
var HTTPClient internal.ContextKey
HTTPClient is the context key to use with golang.org/x/net/context's WithValue function to associate an *http.Client value with a context.
构造上下文时使用上述变量作为上下文键传递给Exchange
:
client := &http.Client{
// TODO
}
parent := oauth2.NoContext()
ctx := context.WithValue(parent, oauth2.HTTPClient, client)
tkn, err := c.Exchange(ctx, code)