LoginSignup
12
9

More than 3 years have passed since last update.

body+で計った体重をWithings APIで取得する

Last updated at Posted at 2018-12-28

Withings body+ を買ったので、APIで体重を取得してみる

APIドキュメント
http://developer.withings.com/

1. withingsアカウントの登録

https://healthmate.withings.com
※ body+ 設定時に登録済みのはず

2. アプリ登録をする

  • 各項目を入力し、アプリ登録
    • コールバックURIは自分の管理下のURLを入れておく
  • 登録後の画面で表示される下記をメモしておく
    • クライアントID
    • コンシューマーシークレット

3. authentication codeの取得

  • URL: https://account.withings.com/oauth2_user/authorize2
  • Parameters :
    • client_id: 2.で取得したクライアントID
    • state: 必要あれば
    • scope: アクセスデータ範囲。今回は user.metrics
    • redirect_uri: 2.で登録したコールバックURI

ブラウザで下記にアクセス

https://account.withings.com/oauth2_user/authorize2?response_type=code&client_id=[クライアントID]&state=[state]&scope=user.metrics&redirect_uri=[callbacl URL]

リダイレクトされたURLからauthentication codeを取得

http://(callbacl URL)/?code=(authentication code)

4. access tokenを取得

  • URL: https://account.withings.com/oauth2/token
  • Parameters :
    • grant_type: authorization_code
    • client_id: 2.で取得したクライアントID
    • client_secret: 2.で取得したコンシューマーシークレット
    • code: 3.で取得した authentication code
    • redirect_uri: 2.で登録したコールバックURI

※ authentication code 取得後、30秒以内にtokenを取得する必要があるそう。

curl でPOSTする

curl --data "grant_type=authorization_code&client_id=[クライアントID]&client_secret=[コンシューマーシークレット]&code=[authentication code]&redirect_uri=[callbacl URL]" 'https://account.withings.com/oauth2/token'
戻り値
{
    "access_token": "xxxxxxx",
    "expires_in": "10800",
    "token_type": "Bearer",
    "scope": "user.metrics",
    "refresh_token": "xxxxxxxxxxx",
    "userid": "xxxxxx"
}

5. 体重を取得する

  • URL: https://wbsapi.withings.net/measure
  • Parameters :
    • action: getmeas
    • meastype: 体重の場合は1
    • category: 今回は1
    • startdate: 取得範囲 timestamp
    • enddate: 取得範囲 timestamp

curlでGETする

curl 'https://wbsapi.withings.net/measure?action=getmeas&access_token=[access token]&meastype=1&category=1&startdate=1543382553&enddate=1545974553'
戻り値
{
    "status": 0,
    "body": {
        "updatetime": 1545974112,
        "timezone": "Asia/Tokyo",
        "measuregrps": [
            {
                "grpid": xxxxxxxxxx,
                "attrib": 0,
                "date": 1545918646,
                "created": 1545918680,
                "category": 1,
                "deviceid": "xxxxxxxxxxx",
                "measures": [
                    {
                        "value": xxxxxx,
                        "type": 1,
                        "unit": -3,
                        "algo": xxxxxx,
                        "fw": xxxxxx,
                        "fm": xxxxxx
                    }
                ],
                "comment": null
            },
            {
                "grpid": xxxxxxxxxx,
                "attrib": 0,
                "date": 1545829628,
                "created": 1545829666,
                "category": 1,
                "deviceid": "xxxxxxxxxx",
                "measures": [
                    {
                        "value": xxxxxx,
                        "type": 1,
                        "unit": -3,
                        "algo": xxxxxx,
                        "fw": xxxxxx,
                        "fm": xxxxxx
                    }
                ],
                "comment": null
            }
        ]
    }
}

取得できた!

返り値の measures.algo, fw, fm あたりは ドキュメントになくて謎。

12
9
2

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
12
9