![見出し画像](https://assets.st-note.com/production/uploads/images/25946214/rectangle_large_type_2_37f0239b34ddea01d009f4b0e4ccffab.jpg?width=1200)
Strava/Withings Web API開発 - 準備編③
Withings(OAUTH認証)
以下リンクに纏められています。流れはStravaと一緒です。
①Withings側のAPI利用設定
以下のURLからアカウントとアプリケーション登録を行います。
https://healthmate.withings.com
https://account.withings.com/partner/add_oauth2
Strava同様に、アプリケーション名(Description)は適当で構いません。
コールバックURLはStravaと同じ設定(http://127.0.0.1:80)としました。
アプリケーション登録後、以下の情報が必要となるので控えておきます。
・クライアントID
・コンシューマーシークレット
②Webサーバー(IIS)の設定
これはStravaで設定済みのためSKIPします。
http://www.floatgarden.net/server/windows10_iis_server.html
③認可コードの発行
下記URLをブラウザより実行します。
※認可コード取得後、30秒以内に④のコマンド実行が必要なので要注意。
https://account.withings.com/oauth2_user/authorize2?response_type=code&client_id=<クライアントID>&state=<任意の文字列>&scope=user.activity,user.metrics,user.info,user.sleepevents&redirect_uri=<コールバックURL>
scopeについては、念のため参照系のパラメーターを全て指定しました。
scope=user.activity,user.metrics,user.info,user.sleepevents
必要なパラメーターは以下の通り。
client_id: クライアントID
state: 任意の文字列
scope: 要求するアクセス権限を指定
-> user.activity,user.metrics,user.info,user.sleepevents
redirect_uri: 認証完了後のリダイレクト先のURL
(今回はhttp://127.0.0.1:80/ を指定)
ブラウザから上記URLを入力すると、①で設定したアプリケーションに対して許可を求められるので、「許可する」を押します。
そうするとリダイレクトURL先へ遷移して、URL上に認可コード(code=xxxxxx...)が表示されます。この値を控えておきます。
④アクセストークンの発行
③で取得した認可コードを使用し、認可サーバにアクセストークンを発行してもらいます。
<実行コマンド>
curl --data "grant_type=authorization_code&client_id=<クライアントID>&client_secret=<コンシューマーシークレット>&code=<認可コード>&redirect_uri=<コールバックURL>" 'https://account.withings.com/oauth2/token'
必要なパラメーターは以下の通り。
client_id: クライアントID
client_secret: コンシューマーシークレット
code: 認可コード(IISのURLのcode=xxxxxx の値)
redirect_uri: コールバックURL
<実行結果>
{"access_token":"xxxxxxx...","expires_in":10800,"token_type":"Bearer","scope":"user.activity,user.metrics,user.info,user.sleepevents","refresh_token":"xxxxxxxxx...","userid":"12345678"}
ここで、access_tokenとrefresh_tokenが発行されます。access_tokenでWithings上の各種データを取得することが可能になり、refresh_tokenでaccess_tokenの期限切れ時の再発行が可能になります。
⑤アクセストークンでのデータ取得
以下コマンドでWithings上のデータ(Body+で測定後に自動アップロードされたデータ)を取得します。
curl 'https://wbsapi.withings.net/measure?action=getmeas&access_token=<access_token>&meastype=1&category=1&startdate=<From日時>&enddate=<To日時>"
必要なパラメーターは以下の通り。
access_token: アクセストークン
startdate: From日時(いつの測定データから取得するか)
enddate: To日時(いつまでの測定データを取得するか)
startdateとenddateはunixtimeで指定します。
(参照)unixtime変換
https://tool.konisimple.net/date/unixtime
例えば、2018年1月1日から2021年1月1日までのデータを取得したい場合は、startdate=1514732400、enddate=1609426800を設定します。
以下通りデータが取得できました。分かりやすいように字下げを行ってますが、実際は横に並んだ形式で出力されます。
{"status":0,"body":
{"updatetime":1589039640,"timezone":"Asia\/Tokyo","measuregrps":
[
{"grpid":1234567890,"attrib":0,"date":1589004819,
"created":1589004852,"category":1,"deviceid":"xxxxxxx...",
"measures":
[{"value":65200,"type":1,"unit":-3,"algo":3,"fm":131}],
"comment":null
},
{"grpid":1234567891,"attrib":0,"date":1588756066,
"created":1588756097,"category":1,"deviceid":"xxxxxxx...",
・・・
ここで、"value":65200と出力されたのが体重になります。(65.2kg)
⑥アクセストークンの再発行(リフレッシュトークンを使用)
トークンの有効期限が切れた場合は認可サーバに再度アクセストークンの発行を依頼します。この時に「リフレッシュトークン」が必要となります。
curl --data "grant_type=refresh_token&client_id=xxxxx...
&client_secret=xxxxx...
&refresh_token=xxxxx..." "https://account.withings.com/oauth2/token"
必要なパラメーターは以下の通り。
grant_type: refresh_token(固定)
client_id: クライアントID
client_secret: コンシューマーシークレット
refresh_token: リフレッシュトークン
このコマンド実行で、アクセストークン/リフレッシュトークンが再発行されます。
{"access_token":"xxxxx...","expires_in":10800,"token_type":"Bearer",
"scope":"user.metrics","refresh_token":"xxxxx...","userid":12345678}
これで各サービスからのデータ取得の準備が整いました。次回からいよいよ開発編になります。(続く)
<関連記事>
Strava/Withings Web API開発(天気と走行距離と体重をダッシュボードで表示してみた)
https://note.com/sanoatsu/n/nbe00ce35d3b1
Strava/Withings Web API開発 - 全体概要
https://note.com/sanoatsu/n/ncc074b9ce5f7
Strava/Withings Web API開発 - 準備編①
https://note.com/sanoatsu/n/n0104f85631cf
Strava/Withings Web API開発 - 準備編②
https://note.com/sanoatsu/n/n4d05a6f1cb2d
Strava/Withings Web API開発 - リファレンス(Web API認証)
https://note.com/sanoatsu/n/n98742c974b17
いいなと思ったら応援しよう!
![「世界のサノアツ」駆ける](https://assets.st-note.com/production/uploads/images/24643794/profile_bcc7945516c7f023a52e2b887c30b6ab.jpg?width=600&crop=1:1,smart)