paypale/auth.go

30 lines
626 B
Go
Raw Normal View History

2015-10-15 10:47:36 +02:00
package paypalsdk
import (
2015-10-30 08:02:32 +01:00
"bytes"
"fmt"
"net/http"
2015-10-15 10:47:36 +02:00
)
2015-10-30 08:02:32 +01:00
// GetAccessToken returns struct of TokenResponse
2015-11-20 07:38:40 +01:00
func (c *Client) GetAccessToken() (*TokenResponse, error) {
buf := bytes.NewBuffer([]byte("grant_type=client_credentials"))
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/oauth2/token"), buf)
2015-10-30 08:02:32 +01:00
if err != nil {
return &TokenResponse{}, err
}
req.SetBasicAuth(c.ClientID, c.Secret)
2015-11-20 08:40:47 +01:00
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
2015-10-30 08:02:32 +01:00
t := TokenResponse{}
err = c.Send(req, &t)
// Set Token fur current Client
if t.Token != "" {
c.Token = &t
}
return &t, err
2015-10-16 12:00:57 +02:00
}