3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Fitbitのアクセストークンを取得する

Posted at

概要

  • Fitbitのデータを取得するためのアクセストークンを取得する方法の備忘録です。

アカウント作成&ログイン

  • Fitbit developerへアクセス

  • Manage -> Register An App をクリック
    image.png

  • Log In をクリック
    image.png

  • アカウントを作成し、SIGN IN する。
    image.png

アプリの登録

  • 以下の設定値で登録する。なお、★の項目は、アクセストークン取得には影響しないので、別のURLでもOKです。

  • 必要項目を入力し、Register をクリック
    image.png

  • アプリの登録完了。

  • OAuth 2.0 Client IDClient Secret はアクセストークン取得後のアクティビティデータ取得時に使うので、メモっておきましょう。(この画面に来れば後からでも確認できます)
    image.png

アクセストークンの発行・取得

  • OAuth 2.0 tutorial page をクリック
    image.png

  • 1: Authorize の Flow type は推奨されている Authorization Code Flow を選択。

  • アプリのデータが入力されているが、編集は不要。
    image.png

  • Scopeはお好みに応じてチェックを外す。今回はフルチェックのままにします。

  • ExpireTimeはデフォルト設定でOK
    image.png

  • 生成された認証URLをクリック
    image.png

  • データのアクセス許可項目を選択し、許可をクリック。(今回はすべてを許可します)
    image.png

  • アプリ作成時に設定したRedirect URLへリダイレクトするので、URLの中のcode部分をコピーする

  • 例: http://localhost:8080/?code=00a30fXXXXXXXXXXXXXXXXXXXXXXXXXXXXX#_=_

    • ここの 00a30fXXXXXXXXXXXXXXXXXXXXXXXXXXXXX がcodeです

image.png

  • OAuth 2.0 tutorial page のタブへ戻り、1A Get Code -> Code に先ほどコピーしたcodeをペーストする。
  • すると、アクセストークンを取得するためのcurlコマンドが表示される。
表示されたcurl
curl -i -X POST \
 -H 'Authorization: Basic YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY' \
 --data "clientId=IIIIIIII" \
 --data "grant_type=authorization_code" \
 --data "redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F" \
 --data "code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
 -H 'Content-Type: application/x-www-form-urlencoded' \
https://api.fitbit.com/oauth2/token

image.png

  • 表示されているcurlコマンドをターミナル等に入力すると、アクセストークンを含んだResponseを受信できる。
  • VSCodeのターミナルでこのままコピペしてもエラーになる場合は、以前書いたcurlコマンドの記事の方法で入力すればOK
受信したResponse
HTTP/1.1 200 OK
Date: Tue, 08 Nov 2022 07:22:09 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Origin,Accept-Encoding
Cache-control: no-cache, private
Set-Cookie: fct=ZZZZZZZZZZZZZZZZZZZZZZZZZZ; Path=/; Secure; HttpOnly
Set-Cookie: JSESSIONID=JJJJJJJJJJJJJJJJ.fitbit1; Path=/; Secure; HttpOnly
Content-Language: ja-JP
X-Frame-Options: SAMEORIGIN
Via: 1.1 google
CF-Cache-Status: DYNAMIC
Server: cloudflare
CF-RAY: 766c8e518e7ae045-NRT

{"access_token":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaa","expires_in":28800,"refresh_token":"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr","scope":"heartrate sleep profile settings temperature nutrition activity respiratory_rate location social weight oxygen_saturation","token_type":"Bearer","user_id":"xxxxxxxxxxxxxxx"}
  • 主要な項目の説明です。

    • access_token : アクセストークン
    • expires_in : アクセストークンの有効期間(秒)。デフォルト設定(変更不可)は288,800秒=8時間。
    • refresh_token : 有効期間が切れたアクセストークンを更新・再取得する際に必要なリフレッシュトークン。
  • お疲れ様でした。無事、アクセストークンの取得完了です。

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?