ClientSecret: "@FITBIT_CLIENT_SECRET@",
Endpoint: oauth2fitbit.Endpoint,
RedirectURL: "https://fitbit-gfit-sync.appspot.com/fitbit/grant",
- Scopes: []string{"activity"},
+ Scopes: []string{"activity", "heartrate", "profile"},
}
const csrfToken = "@CSRFTOKEN@"
}
func NewClient(ctx context.Context, fitbitUserID string, u *app.User) (*Client, error) {
- tok, err := u.Token(ctx, "Fitbit")
- if err != nil {
- return nil, err
- }
-
if fitbitUserID == "" {
fitbitUserID = "-"
}
+ c, err := u.OAuthClient(ctx, "Fitbit", oauth2Config)
+ if err != nil {
+ return nil, err
+ }
+
return &Client{
fitbitUserID: fitbitUserID,
appUser: u,
- client: oauth2Config.Client(ctx, tok),
+ client: c,
}, nil
}
return nil
}
+
+type Profile struct {
+ Name string
+ Timezone *time.Location
+}
+
+func (c *Client) Profile(ctx context.Context) (*Profile, error) {
+ res, err := c.client.Get("https://api.fitbit.com/1/user/-/profile.json")
+ if err != nil {
+ return nil, err
+ }
+ defer res.Body.Close()
+
+ if res.StatusCode >= 400 {
+ data, _ := ioutil.ReadAll(res.Body)
+ log.Errorf(ctx, "reading profile failed: %s", data)
+ return nil, fmt.Errorf("HTTP %d error", res.StatusCode)
+ }
+
+ var data struct {
+ User struct {
+ FullName string
+ OffsetFromUTCMillis int
+ Timezone string
+ }
+ }
+ if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
+ return nil, err
+ }
+
+ loc, err := time.LoadLocation(data.User.Timezone)
+ if err != nil {
+ loc = time.FixedZone("Fitbit preference", data.User.OffsetFromUTCMillis/1000)
+ }
+
+ return &Profile{
+ Name: data.User.FullName,
+ Timezone: loc,
+ }, nil
+}