-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mercadopago/feature/oauth-implementation
Feature/oauth implementation
- Loading branch information
Showing
10 changed files
with
526 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/mercadopago/sdk-go/pkg/config" | ||
"github.com/mercadopago/sdk-go/pkg/oauth" | ||
) | ||
|
||
func main() { | ||
accessToken := "{{ACCESS_TOKEN}}" | ||
|
||
cfg, err := config.New(accessToken) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
client := oauth.NewClient(cfg) | ||
authorizationCode := "{{AUTHORIZATION_CODE}}" | ||
redirectURI := "{{REDIRECT_URI}}" | ||
|
||
cred, err := client.Create(context.Background(), authorizationCode, redirectURI) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println(cred) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mercadopago/sdk-go/pkg/config" | ||
"github.com/mercadopago/sdk-go/pkg/oauth" | ||
) | ||
|
||
func main() { | ||
accessToken := "{{ACCESS_TOKEN}}" | ||
|
||
cfg, err := config.New(accessToken) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
client := oauth.NewClient(cfg) | ||
|
||
clientID := "{{CLIENT_ID}}" | ||
redirectURI := "{{REDIRECT_URI}}" | ||
state := "state" | ||
|
||
url := client.GetAuthorizationURL(clientID, redirectURI, state) | ||
|
||
fmt.Println(url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/mercadopago/sdk-go/pkg/config" | ||
"github.com/mercadopago/sdk-go/pkg/oauth" | ||
) | ||
|
||
func main() { | ||
accessToken := "{{ACCESS_TOKEN}}" | ||
|
||
cfg, err := config.New(accessToken) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
client := oauth.NewClient(cfg) | ||
authorizationCode := "{{AUTHORIZATION_CODE}}" | ||
redirectURI := "{{REDIRECT_URI}}" | ||
|
||
cred, err := client.Create(context.Background(), authorizationCode, redirectURI) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
refreshToken := cred.RefreshToken | ||
cred, err = client.Refresh(context.Background(), refreshToken) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println(cred) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package oauth | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/mercadopago/sdk-go/pkg/config" | ||
"github.com/mercadopago/sdk-go/pkg/internal/baseclient" | ||
) | ||
|
||
const ( | ||
urlBase = "https://api.mercadopago.com/oauth/token" | ||
urlAuth = "https://auth.mercadopago.com/authorization" | ||
) | ||
|
||
// Client contains the method to interact with the Oauth API. | ||
type Client interface { | ||
|
||
// Create oauth credentials to operate on behalf of a seller | ||
// It is a post request to the endpoint: "https://api.mercadopago.com/oauth/token" | ||
// Reference: https://www.mercadopago.com/developers/en/reference/oauth/_oauth_token/post | ||
Create(ctx context.Context, authorizationCode, redirectURI string) (*Response, error) | ||
|
||
// Get url for oauth authorization. | ||
GetAuthorizationURL(clientID, redirectURI, state string) string | ||
|
||
// Refresh token received when you create credentials. | ||
// It is a post request to the endpoint: "https://api.mercadopago.com/oauth/token" | ||
// Reference: https://www.mercadopago.com/developers/en/reference/oauth/_oauth_token/post | ||
Refresh(ctx context.Context, refreshToken string) (*Response, error) | ||
} | ||
|
||
// client is the implementation of Client. | ||
type client struct { | ||
cfg *config.Config | ||
} | ||
|
||
// NewClient returns a new Oauth API Client. | ||
func NewClient(c *config.Config) Client { | ||
return &client{ | ||
cfg: c, | ||
} | ||
} | ||
|
||
func (c *client) Create(ctx context.Context, authorizationCode, redirectURI string) (*Response, error) { | ||
request := &Request{ | ||
ClientSecret: c.cfg.AccessToken, | ||
Code: authorizationCode, | ||
RedirectURI: redirectURI, | ||
GrantType: "authorization_code", | ||
} | ||
|
||
res, err := baseclient.Post[*Response](ctx, c.cfg, urlBase, request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (c *client) GetAuthorizationURL(clientID, redirectURI, state string) string { | ||
params := map[string]string{ | ||
"client_id": clientID, | ||
"response_type": "code", | ||
"platform_id": "mp", | ||
"redirect_uri": redirectURI, | ||
} | ||
|
||
parsedURL, err := url.Parse(urlAuth) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
queryParams := url.Values{} | ||
|
||
for k, v := range params { | ||
queryParams.Add(k, v) | ||
} | ||
|
||
parsedURL.RawQuery = queryParams.Encode() | ||
|
||
return parsedURL.String() | ||
} | ||
|
||
func (c *client) Refresh(ctx context.Context, refreshToken string) (*Response, error) { | ||
request := &RefreshTokenRequest{ | ||
ClientSecret: c.cfg.AccessToken, | ||
RefreshToken: refreshToken, | ||
GrantType: "refresh_token", | ||
} | ||
|
||
res, err := baseclient.Post[*Response](ctx, c.cfg, urlBase, request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.