GetRefreshToken

This commit is contained in:
Aliaksandr Pliutau 2015-10-16 17:00:57 +07:00
parent c0396a16be
commit 0277b266bf
4 changed files with 59 additions and 12 deletions

29
auth.go
View File

@ -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
}

View File

@ -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")
}

View File

@ -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
} }

View File

@ -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