Merge pull request #44 from NicholasAsimov/fix-granttokenfromauthocode

Fix GrantNewAccessTokenFromAuthCode method
This commit is contained in:
Alex Pliutau 2017-10-29 14:44:47 +07:00 committed by GitHub
commit 481eb3337f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 16 deletions

View File

@ -37,11 +37,10 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) {
return &TokenResponse{}, err return &TokenResponse{}, err
} }
req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Content-type", "application/x-www-form-urlencoded") req.Header.Set("Content-type", "application/x-www-form-urlencoded")
t := TokenResponse{} t := TokenResponse{}
err = c.Send(req, &t) err = c.SendWithBasicAuth(req, &t)
// Set Token fur current Client // Set Token fur current Client
if t.Token != "" { if t.Token != "" {
@ -146,6 +145,13 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
return c.Send(req, v) return c.Send(req, v)
} }
// SendWithBasicAuth makes a request to the API using clientID:secret basic auth
func (c *Client) SendWithBasicAuth(req *http.Request, v interface{}) error {
req.SetBasicAuth(c.ClientID, c.Secret)
return c.Send(req, v)
}
// NewRequest constructs a request // NewRequest constructs a request
// Convert payload to a JSON // Convert payload to a JSON
func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) { func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) {

View File

@ -3,25 +3,28 @@ package paypalsdk
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"strings"
) )
// GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code. // GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code.
// Endpoint: POST /v1/identity/openidconnect/tokenservice // Endpoint: POST /v1/identity/openidconnect/tokenservice
func (c *Client) GrantNewAccessTokenFromAuthCode(code string, redirectURI string) (*TokenResponse, error) { func (c *Client) GrantNewAccessTokenFromAuthCode(code string, redirectURI string) (*TokenResponse, error) {
type request struct {
GrantType string `json:"grant_type"`
Code string `json:"code"`
RedirectURI string `json:"redirect_uri"`
}
token := &TokenResponse{} token := &TokenResponse{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), request{GrantType: "authorization_code", Code: code, RedirectURI: redirectURI}) q := url.Values{}
q.Set("grant_type", "authorization_code")
q.Set("code", code)
q.Set("redirect_uri", redirectURI)
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/identity/openidconnect/tokenservice"), strings.NewReader(q.Encode()))
if err != nil { if err != nil {
return token, err return token, err
} }
err = c.SendWithAuth(req, token) req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
err = c.SendWithBasicAuth(req, token)
if err != nil { if err != nil {
return token, err return token, err
} }

View File

@ -1,6 +1,7 @@
package paypalsdk package paypalsdk
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -454,12 +455,14 @@ type (
Phone string `json:"phone,omitempty"` Phone string `json:"phone,omitempty"`
} }
expirationTime int64
// TokenResponse is for API response for the /oauth2/token endpoint // TokenResponse is for API response for the /oauth2/token endpoint
TokenResponse struct { TokenResponse struct {
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`
Token string `json:"access_token"` Token string `json:"access_token"`
Type string `json:"token_type"` Type string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"` ExpiresIn expirationTime `json:"expires_in"`
} }
// Transaction struct // Transaction struct
@ -480,14 +483,14 @@ type (
GivenName string `json:"given_name"` GivenName string `json:"given_name"`
FamilyName string `json:"family_name"` FamilyName string `json:"family_name"`
Email string `json:"email"` Email string `json:"email"`
Verified bool `json:"verified,omitempty"` Verified bool `json:"verified,omitempty,string"`
Gender string `json:"gender,omitempty"` Gender string `json:"gender,omitempty"`
BirthDate string `json:"birthdate,omitempty"` BirthDate string `json:"birthdate,omitempty"`
ZoneInfo string `json:"zoneinfo,omitempty"` ZoneInfo string `json:"zoneinfo,omitempty"`
Locale string `json:"locale,omitempty"` Locale string `json:"locale,omitempty"`
Phone string `json:"phone_number,omitempty"` Phone string `json:"phone_number,omitempty"`
Address *Address `json:"address,omitempty"` Address *Address `json:"address,omitempty"`
VerifiedAccount bool `json:"verified_account,omitempty"` VerifiedAccount bool `json:"verified_account,omitempty,string"`
AccountType string `json:"account_type,omitempty"` AccountType string `json:"account_type,omitempty"`
AgeRange string `json:"age_range,omitempty"` AgeRange string `json:"age_range,omitempty"`
PayerID string `json:"payer_id,omitempty"` PayerID string `json:"payer_id,omitempty"`
@ -544,3 +547,17 @@ func (t JSONTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%s"`, time.Time(t).UTC().Format(time.RFC3339)) stamp := fmt.Sprintf(`"%s"`, time.Time(t).UTC().Format(time.RFC3339))
return []byte(stamp), nil return []byte(stamp), nil
} }
func (e *expirationTime) UnmarshalJSON(b []byte) error {
var n json.Number
err := json.Unmarshal(b, &n)
if err != nil {
return err
}
i, err := n.Int64()
if err != nil {
return err
}
*e = expirationTime(i)
return nil
}