LoginSignup
1
1

More than 3 years have passed since last update.

curlコマンドでfitbit APIを叩いてみる

Last updated at Posted at 2020-02-21

はじめに

curlコマンドを使ってFitbit APIを叩いてみたので、忘れないようにメモしておきます。
Fitbitデベロッパーサイトからaccess_tokenを取得します。下図の赤枠の部分。
Screen Shot 2020-02-21 at 16.51.43.png

プロファイルを取得

$ curl -H "Authorization: Bearer [your token]" https://api.fitbit.com/1/user/-/profile.json

レスポンスを見易くするためjqをインストールすると良いです。

$ brew install jq

歩数を取得

今日から1ヶ月前までの歩数を取得します。

$ curl -H "Authorization: Bearer [your token]" https://api.fitbit.com/1/user/-/activities/steps/date/today/1m.json | jq
{
  "activities-steps": [
    {
      "dateTime": "2020-01-22",
      "value": "6357"
    },
    {
      "dateTime": "2020-01-23",
      "value": "6405"
    },
...
    {
      "dateTime": "2020-02-21",
      "value": "2270"
    }
  ]
}

食事を記録する

食事を記録するには、Fitbit食品データベースからfoodIdを取得する必要があります。
テストでは「タピオカ」を検索ワードにしています。
検索ワードはエンコードしてPOSTしてあげなければエラーとなります。
エンコード方法はググるといろんなサイトが出てきます。

curl -X POST -H "Authorization: Bearer [your token]" https://api.fitbit.com/1/foods/search.json?query=%E3%82%BF%E3%83%94%E3%82%AA%E3%82%AB

query以下が検索ワードです。

{
  "foods": [
    {
      "accessLevel": "PUBLIC",
      "brand": "",
      "calories": 212,
      "defaultServingSize": 1,
      "defaultUnit": {
        "id": 304,
        "name": "食分",
        "plural": "食分"
      },
      "foodId": 19297945,
      "isGeneric": false,
      "locale": "ja_JP",
      "name": "タピオカフルーツ",
      "units": [
        304
      ]
    },
    {
      "accessLevel": "PUBLIC",
      "brand": "",
      "calories": 300,
      "defaultServingSize": 1,
      "defaultUnit": {
        "id": 304,
        "name": "食分",
        "plural": "食分"
      },
      "foodId": 19297918,
      "isGeneric": false,
      "locale": "ja_JP",
      "name": "タピオカアイス",
      "units": [
        304
      ]
    },
...
    {
      "accessLevel": "PUBLIC",
      "brand": "",
      "calories": 275,
      "defaultServingSize": 1,
      "defaultUnit": {
        "id": 304,
        "name": "食分",
        "plural": "食分"
      },
      "foodId": 21863417,
      "isGeneric": false,
      "locale": "ja_JP",
      "name": "タピオカフロート ココア(ファーストキッチン)",
      "units": [
        304
      ]
    }
  ]
}

いっぱいありますね!適当なfoodIdを拾ってきましょう。
ここでは"21863417"を使います。
それでは食事を登録します。

curl -X POST \
-H "Authorization: Bearer [your token]" \
--data "foodId=21863417&date=2020-02-21&amount=2&unitId=304&mealTypeId=7&favorite=false" \
https://api.fitbit.com/1/user/-/foods/log.json

IMG_6217.PNG IMG_6218.PNG
ちゃんとタピオカフロート ココア(・・・が登録されましたね!

参考

Fitbit API
curlコマンド使い方

今回はここまでです。

1
1
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
1
1