diff --git a/adapters/http/http.go b/adapters/http/http.go index e415597..b0c3804 100644 --- a/adapters/http/http.go +++ b/adapters/http/http.go @@ -25,9 +25,9 @@ type httpAdapter struct { var ( // ErrorNilData no data - ErrorNilData = errors.New("No data to parse ") + ErrorNilData = errors.New("no data to parse ") // ErrorNilAuth no auth service - ErrorNilAuth = errors.New("No auth to parse ") + ErrorNilAuth = errors.New("no auth to parse ") ) // New returns an httpAdapter @@ -36,7 +36,8 @@ func New(client HTTPClient, baseUrl string) *httpAdapter { } // DoRequest is the httpAdapter requester -func (h *httpAdapter) DoRequest(method, path string, reader io.Reader, headers map[string]string) ([]byte, int, error) { +func (h *httpAdapter) DoRequest(method, path string, reader io.Reader, headers map[string]string) (response []byte, status int, err error) { + glg.Debugf("[DoRequest]: method:%v, url:%v\n", method, h.baseUrl+path) request, err := http.NewRequest(method, h.baseUrl+path, reader) if err != nil { return nil, 0, err diff --git a/examples/auth/main.go b/examples/auth/main.go new file mode 100644 index 0000000..d04754c --- /dev/null +++ b/examples/auth/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "log" + "os" + + sdk "github.com/arxdsilva/golang-ifood-sdk/container" +) + +func main() { + var clientID, clientSecret string + clientID = os.Getenv("CLIENT_ID") + clientSecret = os.Getenv("CLIENT_SECRET") + // START new SDK instance + c := sdk.Create(clientID, clientSecret, 0, true) + // Get user code to connect this supplier to the restaurant + uc, err := c.AuthService.V2GetUserCode() + if err != nil { + log.Fatal(err) + } + fmt.Printf("v2 user: %+v\n", uc) + v2Creds, err := c.AuthService.V2Authenticate( + "client_credentials", + uc.Usercode, uc.AuthorizationCodeVerifier, "") + if err != nil { + log.Fatal(err) + } + fmt.Printf("v2 creds: %+v\n", v2Creds) +} diff --git a/services/authentication/authentication.go b/services/authentication/authentication.go index 61c4cca..afa2178 100644 --- a/services/authentication/authentication.go +++ b/services/authentication/authentication.go @@ -8,7 +8,6 @@ import ( "mime/multipart" "net/http" "net/url" - "strconv" "strings" "time" @@ -17,6 +16,7 @@ import ( ) const ( + authRoot = "/authentication/v1.0" authEndpoint = "/oauth/token" userCodeEndpoint = "/oauth/userCode" valueGrantType = "password" @@ -76,8 +76,6 @@ type ( currentExpiration time.Time Token string refreshToken string - authCode string - authCodeVerifier string v2 bool } ) @@ -88,18 +86,18 @@ func New(adapter adapters.Http, clientId, clientSecret string, v2 bool) Service } func (a *authService) V2GetUserCode() (uc *UserCode, err error) { - data := url.Values{} - data.Set("client_id", a.clientId) + params := url.Values{} + params.Add("clientId", a.clientId) + body := strings.NewReader(params.Encode()) headers := make(map[string]string) headers["Content-Type"] = "application/x-www-form-urlencoded" - headers["Content-Length"] = strconv.Itoa(len(data.Encode())) - body := strings.NewReader(data.Encode()) - resp, status, err := a.adapter.DoRequest(http.MethodPost, userCodeEndpoint, body, headers) + resp, status, err := a.adapter.DoRequest(http.MethodPost, authRoot+userCodeEndpoint, body, headers) if err != nil { glg.Error("[SDK] (V2GetUserCode::DoRequest) error: ", err.Error()) return } if status != http.StatusOK { + glg.Debug("[SDK] (V2GetUserCode::status.check): resp ", string(resp)) glg.Warn("[SDK] (V2GetUserCode::status.check): status code ", status) err = ErrUnauthorized return @@ -127,9 +125,8 @@ func (a *authService) V2Authenticate(authType, authCode, authCodeVerifier, refre data.Set("refreshToken", refreshToken) headers := make(map[string]string) headers["Content-Type"] = "application/x-www-form-urlencoded" - headers["Content-Length"] = strconv.Itoa(len(data.Encode())) body := strings.NewReader(data.Encode()) - resp, status, err := a.adapter.DoRequest(http.MethodPost, authEndpoint, body, headers) + resp, status, err := a.adapter.DoRequest(http.MethodPost, authRoot+authEndpoint, body, headers) if err != nil { glg.Error("[SDK] (V2Authenticate::DoRequest) error: ", err.Error()) return