LoginSignup
6
7

More than 5 years have passed since last update.

WindowsでFitbitのWebAPIを試してみる

Posted at

概要

Fitbitにデビューして早2年。アプリやWebで自分の記録を見てたけど、2年も経つと過去の見たい記録がなかなか見つからなくなったため、FitbitのWebAPIをちょっと調べてみた
ここでは、アプリでよく見る歩数、階数、距離、カロリー、エクササイズ記録、体重、体脂肪率、睡眠ステージ、心拍数ゾーンのAPIを記載

できるようになること

HTTPSリクエストでFitbitの以下の情報を取得できるようになる

  • 歩数、階数、距離、カロリー
  • エクササイズ記録
  • 体重
  • 体脂肪率
  • 睡眠ステージ
  • 心拍数ゾーン

参考ページ

[本家のページ]
 Fitbit WebAPI

事前準備

最低限以下の2点が必要

  • HTTPSのGETリクエストが送れること
  • 認証情報(Authorization)をHTTPヘッダに設定できること

認証情報は以下のページ等を参考にWindowsで「ブラウザ」と「コマンドプロンプトでのcurlのコマンド実行」で取得できる

 FitbitのAPIを手っ取り早く試してみる方法

実際のWebAPIのRAWは↓の感じ
xxxxxxに上記で取得した認証情報を設定する

GET https://api.fitbit.com/1/user/-/activities/date/2017-11-12.json HTTP/1.1
Accept-Encoding: gzip,deflate
Authorization: Bearer xxxxxx
Host: api.fitbit.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

歩数、階数、距離、カロリー取得

使用API

image.png

APIサンプル

https://api.fitbit.com/1/user/-/activities/date/2019-03-03.json

レスポンスサンプル

image.png

JSON取得サンプル

    jsonstr['summary']['steps'] # 歩数
    jsonstr['summary']['floors'] # 階数
    jsonstr['summary']['distances'][2]['distance'] # 距離
    jsonstr['summary']['caloriesOut'] # カロリー

エクササイズ記録取得

使用API

image.png
(使用APIは「歩数、階数、距離、カロリー」と同じ)

APIサンプル

https://api.fitbit.com/1/user/-/activities/date/2019-03-03.json

レスポンスサンプル

image.png

JSON取得サンプル

    jsonstr['activities'][0]

体重取得

使用API

image.png

APIサンプル

https://api.fitbit.com/1/user/-/body/log/weight/date/2019-02-24.json

レスポンスサンプル

image.png

JSON取得サンプル

    jsonstr['weight'][0]['weight']

体脂肪率取得

使用API

image.png

APIサンプル

https://api.fitbit.com/1/user/-/body/log/fat/date/2019-02-24.json

レスポンスサンプル

image.png

JSON取得サンプル

    jsonstr['fat'][0]['fat']

睡眠ステージ取得

使用API

image.png

APIサンプル

https://api.fitbit.com/1/user/-/sleep/date/2019-03-01.json

レスポンスサンプル

image.png

JSON取得サンプル

    jsonstr['summary']

心拍数ゾーン取得

使用API

image.png

APIサンプル

https://api.fitbit.com/1/user/-/activities/heart/date/2019-03-01/1d.json

レスポンスサンプル

image.png

JSON取得サンプル

    jsonstr['activities-heart'][0]['value']['heartRateZones']

以上。

注意事項

アクセストークンごとに150リクエスト/1時間の制限あり

image.png

参考情報

プログラムで作成する場合、各種ライブラリもあり

 Community Resources

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7