mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
feat: get access token automatically (#261)
This commit is contained in:
parent
26473c3630
commit
5fddf59f71
|
@ -119,8 +119,6 @@ import "github.com/plutov/paypal/v4"
|
||||||
// Create a client instance
|
// Create a client instance
|
||||||
c, err := paypal.NewClient("clientID", "secretID", paypal.APIBaseSandBox)
|
c, err := paypal.NewClient("clientID", "secretID", paypal.APIBaseSandBox)
|
||||||
c.SetLog(os.Stdout) // Set log to terminal stdout
|
c.SetLog(os.Stdout) // Set log to terminal stdout
|
||||||
|
|
||||||
accessToken, err := c.GetAccessToken(context.Background())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Get authorization by ID
|
## Get authorization by ID
|
||||||
|
|
|
@ -143,8 +143,7 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
||||||
// Note: Here we do not want to `defer c.Unlock()` because we need `c.Send(...)`
|
// Note: Here we do not want to `defer c.Unlock()` because we need `c.Send(...)`
|
||||||
// to happen outside of the locked section.
|
// to happen outside of the locked section.
|
||||||
|
|
||||||
if c.Token != nil {
|
if c.Token == nil || (!c.tokenExpiresAt.IsZero() && time.Until(c.tokenExpiresAt) < RequestNewTokenBeforeExpiresIn) {
|
||||||
if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn {
|
|
||||||
// c.Token will be updated in GetAccessToken call
|
// c.Token will be updated in GetAccessToken call
|
||||||
if _, err := c.GetAccessToken(req.Context()); err != nil {
|
if _, err := c.GetAccessToken(req.Context()); err != nil {
|
||||||
// c.Unlock()
|
// c.Unlock()
|
||||||
|
@ -154,7 +153,6 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
||||||
}
|
|
||||||
// Unlock the client mutex before sending the request, this allows multiple requests
|
// Unlock the client mutex before sending the request, this allows multiple requests
|
||||||
// to be in progress at the same time.
|
// to be in progress at the same time.
|
||||||
// c.Unlock()
|
// c.Unlock()
|
||||||
|
|
|
@ -65,9 +65,7 @@ func (c *Client) sendWithAuth(req *http.Request, v interface{}) error {
|
||||||
err := errors.New("TryLock succeeded inside sendWithAuth with mutex locked")
|
err := errors.New("TryLock succeeded inside sendWithAuth with mutex locked")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if c.Token == nil || (!c.tokenExpiresAt.IsZero() && time.Until(c.tokenExpiresAt) < RequestNewTokenBeforeExpiresIn) {
|
||||||
if c.Token != nil {
|
|
||||||
if !c.tokenExpiresAt.IsZero() && c.tokenExpiresAt.Sub(time.Now()) < RequestNewTokenBeforeExpiresIn {
|
|
||||||
// c.Token will be updated in GetAccessToken call
|
// c.Token will be updated in GetAccessToken call
|
||||||
if _, err := c.GetAccessToken(req.Context()); err != nil {
|
if _, err := c.GetAccessToken(req.Context()); err != nil {
|
||||||
// c.Unlock()
|
// c.Unlock()
|
||||||
|
@ -75,9 +73,7 @@ func (c *Client) sendWithAuth(req *http.Request, v interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
||||||
}
|
|
||||||
// Unlock the client mutex before sending the request, this allows multiple requests
|
// Unlock the client mutex before sending the request, this allows multiple requests
|
||||||
// to be in progress at the same time.
|
// to be in progress at the same time.
|
||||||
// c.Unlock()
|
// c.Unlock()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user