Question about client_credentials #940
-
This is not an issue but a question. I understand that with net/http and clientcredentials we can get data from any endpoint that requires Oauth2 access token. It "internally" arranges for refresh of token and getting the data with that fresh token. Can we do such thing with resty? Please excuse me ........I am not very deep on ssl part |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Is your question about how to use this https://pkg.go.dev/golang.org/x/oauth2/clientcredentials package with Resty?
To add CA certs (root or client root), you could use these methods.
Also, explore godoc to know more about Resty - https://pkg.go.dev/github.com/go-resty/resty/v2 and README.md file. |
Beta Was this translation helpful? Give feedback.
-
Yes. With this package we can set grant_type, client_id, client_secret and token url in the client config and the package takes care of obtaining new or refreshing old token and get the data too ..........without us getting the token and setting it on the request header. Moreover I want to use the above with setting the CA cert ......... So please see if you have any example that does these two things
Yes I have read about those methods in your documentation I hope I answered your question |
Beta Was this translation helpful? Give feedback.
-
Using clientcredentials package in the Resty is simple. clientCredCfg := &Config{ /* values ... */ }
client := resty.NewWithClient(clientCredCfg.Client(context.Background()))
// start using resty client ... However, there is a catch in the next part of the question. i.e., using CA Cert. As per the GoDoc of clientcredentials#Config.Client
So this is no-go route. I would suggest using the following approach. Example using Resty v2.16.2 clientCredCfg := &Config{ /* values ... */ }
client := resty.New()
// Using PreRequestHook set valid token on each request
client.SetPreRequestHook(func(_ *Client, req *http.Request) error {
token := clientCredCfg.Token(req.Context())
token.SetAuthHeader(req)
})
// Set the root CA certs using the mentioned methods from the previous answer.
// start using resty client Above code snippet brings the similar behavior of setting a valid token on each request. |
Beta Was this translation helpful? Give feedback.
@MaheshVelankar
Is your question about how to use this https://pkg.go.dev/golang.org/x/oauth2/clientcredentials package with Resty?
To add CA certs (root or client root), you could use these methods.
Also, explore godoc to know more about Resty - https://pkg.go.d…