mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
GetRefreshToken
This commit is contained in:
parent
c0396a16be
commit
0277b266bf
29
auth.go
29
auth.go
|
@ -1,9 +1,12 @@
|
||||||
package paypalsdk
|
package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"net/url"
|
"net/url"
|
||||||
"errors"
|
"errors"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
||||||
|
@ -21,3 +24,29 @@ func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (s
|
||||||
url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") +
|
url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") +
|
||||||
"&redirect_uri=" + url.QueryEscape(redirectURI), nil
|
"&redirect_uri=" + url.QueryEscape(redirectURI), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRefreshToken returns struct of RefreshTokenResponse
|
||||||
|
// Client must to get an authorization code before
|
||||||
|
// redirectURI must match with redirectURI sent to GetAuthorizationCodeURL
|
||||||
|
func (c *Client) GetRefreshToken(authorizationCode string, redirectURI string) (*RefreshTokenResponse, error) {
|
||||||
|
if authorizationCode == "" {
|
||||||
|
return &RefreshTokenResponse{}, errors.New("authorizationCode cannot be empty")
|
||||||
|
}
|
||||||
|
if redirectURI == "" {
|
||||||
|
return &RefreshTokenResponse{}, errors.New("redirectURI cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte("grant_type=authorization_code&code=" + url.QueryEscape(authorizationCode) + "&redirect_uri=" + url.QueryEscape(redirectURI)))
|
||||||
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), buf)
|
||||||
|
if err != nil {
|
||||||
|
return &RefreshTokenResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.SetBasicAuth(c.ClientID, c.Secret)
|
||||||
|
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
r := RefreshTokenResponse{}
|
||||||
|
err = c.Send(req, &r)
|
||||||
|
|
||||||
|
return &r, err
|
||||||
|
}
|
||||||
|
|
16
auth_test.go
16
auth_test.go
|
@ -22,3 +22,19 @@ func TestGetAuthorizationCodeURL(t *testing.T) {
|
||||||
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test and scope=address")
|
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test and scope=address")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetRefreshToken(t *testing.T) {
|
||||||
|
c, _ := NewClient("clid", "secret", APIBaseSandBox)
|
||||||
|
|
||||||
|
_, err := c.GetRefreshToken("123", "")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("redirectURI is required in GetRefreshToken")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = c.GetRefreshToken("", "123")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("authorizationCode is required in GetRefreshToken")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = c.GetRefreshToken("123", "123")
|
||||||
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"errors"
|
"errors"
|
||||||
"bytes"
|
"bytes"
|
||||||
"time"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
@ -31,7 +30,7 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) {
|
||||||
buf := bytes.NewBuffer([]byte("grant_type=client_credentials"))
|
buf := bytes.NewBuffer([]byte("grant_type=client_credentials"))
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/oauth2/token"), buf)
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/oauth2/token"), buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return &TokenResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.SetBasicAuth(c.ClientID, c.Secret)
|
req.SetBasicAuth(c.ClientID, c.Secret)
|
||||||
|
@ -39,9 +38,6 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) {
|
||||||
|
|
||||||
t := TokenResponse{}
|
t := TokenResponse{}
|
||||||
err = c.Send(req, &t)
|
err = c.Send(req, &t)
|
||||||
if err == nil {
|
|
||||||
t.ExpiresAt = time.Now().Add(time.Duration(t.ExpiresIn/2) * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &t, err
|
return &t, err
|
||||||
}
|
}
|
||||||
|
|
20
types.go
20
types.go
|
@ -2,7 +2,6 @@ package paypalsdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,12 +26,19 @@ type (
|
||||||
|
|
||||||
// TokenResponse maps to the API response for the /oauth2/token endpoint
|
// TokenResponse maps to the API response for the /oauth2/token endpoint
|
||||||
TokenResponse struct {
|
TokenResponse struct {
|
||||||
Scope string `json:"scope"` // "https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.*",
|
Scope string `json:"scope"`
|
||||||
Token string `json:"access_token"` // "EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG",
|
Token string `json:"access_token"`
|
||||||
Type string `json:"token_type"` // "Bearer",
|
Type string `json:"token_type"`
|
||||||
AppID string `json:"app_id"` // "APP-6XR95014BA15863X",
|
AppID string `json:"app_id"`
|
||||||
ExpiresIn int `json:"expires_in"` // 28800
|
ExpiresIn int `json:"expires_in"`
|
||||||
ExpiresAt time.Time `json:"expires_at"`
|
}
|
||||||
|
|
||||||
|
// RefreshTokenResponse maps to the API response for the /v1/identity/openidconnect/tokenservice
|
||||||
|
RefreshTokenResponse struct {
|
||||||
|
Type string `json:"token_type"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
ExpiresIn int `json:"expires_in"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorResponse is used when a response has errors
|
// ErrorResponse is used when a response has errors
|
||||||
|
|
Loading…
Reference in New Issue
Block a user