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
This commit is contained in:
Nicholas Asimov 2017-10-21 16:17:58 +03:00
parent ac5b4d7658
commit 06b13fe21f
2 changed files with 19 additions and 10 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
} }