From c2705dc5eefdfc40f422227cdfd5ff52d4b115e3 Mon Sep 17 00:00:00 2001 From: Nicholas Asimov Date: Sat, 21 Oct 2017 16:17:58 +0300 Subject: [PATCH] Fix GrantNewAccessTokenFromAuthCode method. Changes made: 1. Body is sent as a form-urlencoded values instead of json 2. Basic auth is used for authorization instead of an access token --- client.go | 10 ++++++++-- identity.go | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 59ab951..db8bb5e 100644 --- a/client.go +++ b/client.go @@ -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 != "" { @@ -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) { diff --git a/identity.go b/identity.go index de6a591..e2dd304 100644 --- a/identity.go +++ b/identity.go @@ -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 }