mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
GetAuthorizationCodeURL
This commit is contained in:
parent
edbe51d686
commit
c0396a16be
23
auth.go
Normal file
23
auth.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package paypalsdk
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"net/url"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// GetAuthorizationCodeURL returns URL where we need to redirect user
|
||||
// After signin in PayPal get authorization_code on redirectURI
|
||||
func (c *Client) GetAuthorizationCodeURL(redirectURI string, scopes []string) (string, error) {
|
||||
if redirectURI == "" {
|
||||
return "", errors.New("redirectURI cannot be empty")
|
||||
}
|
||||
|
||||
if len(scopes) == 0 {
|
||||
scopes = []string{"profile", "email"}
|
||||
}
|
||||
|
||||
return c.APIBase + "/webapps/auth/protocol/openidconnect/v1/authorize?client_id=" +
|
||||
url.QueryEscape(c.ClientID) + "&response_type=code&scope=" + strings.Join(scopes, "+") +
|
||||
"&redirect_uri=" + url.QueryEscape(redirectURI), nil
|
||||
}
|
24
auth_test.go
Normal file
24
auth_test.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package paypalsdk
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetAuthorizationCodeURL(t *testing.T) {
|
||||
c, _ := NewClient("clid", "secret", APIBaseSandBox)
|
||||
|
||||
_, err := c.GetAuthorizationCodeURL("", []string{})
|
||||
if err == nil {
|
||||
t.Errorf("redirectURI is required in GetAuthorizationCodeURL")
|
||||
}
|
||||
|
||||
uri, err := c.GetAuthorizationCodeURL("test", []string{})
|
||||
if uri != "https://api.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clid&response_type=code&scope=profile+email&redirect_uri=test" {
|
||||
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test")
|
||||
}
|
||||
|
||||
uri, err = c.GetAuthorizationCodeURL("test", []string{"address"})
|
||||
if uri != "https://api.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clid&response_type=code&scope=address&redirect_uri=test" {
|
||||
t.Errorf("GetAuthorizationCodeURL returns incorrect value for redirectURI=test and scope=address")
|
||||
}
|
||||
}
|
5
types.go
5
types.go
|
@ -6,12 +6,13 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
|
||||
const (
|
||||
// APIBaseSandBox points to the sandbox (for testing) version of the API
|
||||
APIBaseSandBox = "https://api.sandbox.paypal.com/v1"
|
||||
APIBaseSandBox = "https://api.sandbox.paypal.com"
|
||||
|
||||
// APIBaseLive points to the live version of the API
|
||||
APIBaseLive = "https://api.paypal.com/v1"
|
||||
APIBaseLive = "https://api.paypal.com"
|
||||
)
|
||||
|
||||
type (
|
||||
|
|
Loading…
Reference in New Issue
Block a user