forked from go-packages/paypal
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:
parent
ac5b4d7658
commit
06b13fe21f
10
client.go
10
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) {
|
||||
|
|
19
identity.go
19
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user