From 44a8f8e593dc89f178189d94f01d052433109e58 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 14 Apr 2017 18:19:12 +0900 Subject: [PATCH] set urn:ietf:wg:oauth:2.0:oob if RedirectURIs is not set --- mastodon.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/mastodon.go b/mastodon.go index a05bf67..a82a39f 100644 --- a/mastodon.go +++ b/mastodon.go @@ -48,7 +48,7 @@ func (c *Client) doAPI(method string, uri string, params url.Values, res interfa return nil } - if method == "GET" && resp.StatusCode != 200 { + if method == "GET" && resp.StatusCode != http.StatusOK { return fmt.Errorf("bad request: %v", resp.Status) } @@ -90,7 +90,7 @@ func (c *Client) Authenticate(username, password string) error { } defer resp.Body.Close() - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return fmt.Errorf("bad authorization: %v", resp.Status) } @@ -133,7 +133,11 @@ type Application struct { func RegisterApp(appConfig *AppConfig) (*Application, error) { params := url.Values{} params.Set("client_name", appConfig.ClientName) - params.Set("redirect_uris", appConfig.RedirectURIs) + if appConfig.RedirectURIs == "" { + params.Set("redirect_uris", "urn:ietf:wg:oauth:2.0:oob") + } else { + params.Set("redirect_uris", appConfig.RedirectURIs) + } params.Set("scopes", appConfig.Scopes) params.Set("website", appConfig.Website) @@ -147,19 +151,24 @@ func RegisterApp(appConfig *AppConfig) (*Application, error) { if err != nil { return nil, err } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := appConfig.Do(req) if err != nil { return nil, err } defer resp.Body.Close() - app := &Application{} - err = json.NewDecoder(resp.Body).Decode(app) + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("bad request: %v", resp.Status) + } + + var app Application + err = json.NewDecoder(resp.Body).Decode(&app) if err != nil { return nil, err } - return app, nil + return &app, nil } // Account hold information for mastodon account.