From 52cd11fdc84ec2299ba13de1dfbf47b956cd12bf Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 30 Jan 2018 10:09:36 +0100 Subject: [PATCH] Unsubscribe from Fitbit subscriptions individually. Unsubscribing from the generic URL fails with insufficient OAuth scopes. *sigh* --- fitbit/fitbit.go | 15 +++++++-------- kraftakt.go | 8 ++++++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/fitbit/fitbit.go b/fitbit/fitbit.go index c7a6aa7..775ab78 100644 --- a/fitbit/fitbit.go +++ b/fitbit/fitbit.go @@ -17,7 +17,6 @@ import ( "google.golang.org/appengine/log" ) - func oauthConfig() *oauth2.Config { return &oauth2.Config{ ClientID: app.Config.FitbitClientID, @@ -193,21 +192,21 @@ func (c *Client) Subscribe(ctx context.Context, collection string) error { if res.StatusCode >= 400 && res.StatusCode != http.StatusConflict { data, _ := ioutil.ReadAll(res.Body) - log.Errorf(ctx, "creating subscription failed: status %d %q", res.StatusCode, data) - return fmt.Errorf("creating subscription failed") + log.Errorf(ctx, "creating %q subscription failed: status %d %q", collection, res.StatusCode, data) + return fmt.Errorf("creating %q subscription failed", collection) } return nil } -func (c *Client) Unsubscribe(ctx context.Context) error { +func (c *Client) Unsubscribe(ctx context.Context, collection string) error { subscriberID, err := c.appUser.ID(ctx) if err != nil { return err } - url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/apiSubscriptions/%s.json", - c.fitbitUserID, subscriberID) + url := fmt.Sprintf("https://api.fitbit.com/1/user/%s/%s/apiSubscriptions/%s.json", + c.fitbitUserID, collection, subscriberID) req, err := http.NewRequest(http.MethodDelete, url, nil) if err != nil { return err @@ -221,8 +220,8 @@ func (c *Client) Unsubscribe(ctx context.Context) error { if res.StatusCode >= 400 && res.StatusCode != http.StatusConflict { data, _ := ioutil.ReadAll(res.Body) - log.Errorf(ctx, "creating subscription failed: status %d %q", res.StatusCode, data) - return fmt.Errorf("deleting subscription failed") + log.Errorf(ctx, "deleting %q subscription failed: status %d %q", collection, res.StatusCode, data) + return fmt.Errorf("deleting %q subscription failed", collection) } return nil diff --git a/kraftakt.go b/kraftakt.go index 95d7b46..87b8bf2 100644 --- a/kraftakt.go +++ b/kraftakt.go @@ -172,8 +172,12 @@ func fitbitDisconnectHandler(ctx context.Context, w http.ResponseWriter, r *http } var errs appengine.MultiError - if err := c.Unsubscribe(ctx); err != nil { - errs = append(errs, fmt.Errorf("Unsubscribe() = %v", err)) + + for _, collection := range []string{"activities", "sleep"} { + if err := c.Unsubscribe(ctx, collection); err != nil { + errs = append(errs, fmt.Errorf("Unsubscribe(%q) = %v", collection, err)) + } + log.Infof(ctx, "Successfully unsubscribed from %q", collection) } if err := c.DeleteToken(ctx); err != nil { -- 2.11.0