Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GrantNewAccessTokenFromAuthCode method #44

Merged
merged 3 commits into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) {
return &TokenResponse{}, err
}

req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")

t := TokenResponse{}
err = c.Send(req, &t)
err = c.SendWithBasicAuth(req, &t)

// Set Token fur current Client
if t.Token != "" {
Expand Down Expand Up @@ -146,6 +145,13 @@ func (c *Client) SendWithAuth(req *http.Request, v interface{}) error {
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
// Convert payload to a JSON
func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Request, error) {
Expand Down
19 changes: 11 additions & 8 deletions identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ package paypalsdk
import (
"fmt"
"net/http"
"net/url"
"strings"
)

// GrantNewAccessTokenFromAuthCode - Use this call to grant a new access token, using the previously obtained authorization code.
// Endpoint: POST /v1/identity/openidconnect/tokenservice
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{}

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 {
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 {
return token, err
}
Expand Down
29 changes: 23 additions & 6 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package paypalsdk

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -454,12 +455,14 @@ type (
Phone string `json:"phone,omitempty"`
}

expirationTime int64

// TokenResponse is for API response for the /oauth2/token endpoint
TokenResponse struct {
RefreshToken string `json:"refresh_token"`
Token string `json:"access_token"`
Type string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Token string `json:"access_token"`
Type string `json:"token_type"`
ExpiresIn expirationTime `json:"expires_in"`
}

// Transaction struct
Expand All @@ -480,14 +483,14 @@ type (
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Email string `json:"email"`
Verified bool `json:"verified,omitempty"`
Verified bool `json:"verified,omitempty,string"`
Gender string `json:"gender,omitempty"`
BirthDate string `json:"birthdate,omitempty"`
ZoneInfo string `json:"zoneinfo,omitempty"`
Locale string `json:"locale,omitempty"`
Phone string `json:"phone_number,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"`
AgeRange string `json:"age_range,omitempty"`
PayerID string `json:"payer_id,omitempty"`
Expand Down Expand Up @@ -544,3 +547,17 @@ func (t JSONTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%s"`, time.Time(t).UTC().Format(time.RFC3339))
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
}