Fitbit login

This tutorial will guild you how to fitbit login, first things first, you must have fitbit account, create a application to receive client id and secret, for more details please see this create fitbit app.

Once you have client id and secret, please read the instructions below to get start with fitbit login.

Note: Fitbit login using OAuth 2.0, for more information please read this article.

System requirements

  • PHP 5.4 or higher
  • APACHE 2.4.9 or higher
  • Netbean IDE

Project Structure

Implement code

Create projecthelper.php

We wrote one tutorial about projecthelper.php, view more at here, if you were already read it, please add this function in file.

  • public static function get_fitbit_config() {
    return $config = array(
    'client_id' => 'your-client-id',
    'client_secret' => 'client-secret',
    'redirect_uri' => self::get_base_url('fitbit-callback.php'),
    'login' => self::get_base_url('fitbit-login.php'),
    'session' => 'fitbit_access_token'
    );
    }

Create fitbit-login.php

This file will undertake two main tasks, check access_token in $_SESSION, if existed, send request to get user's profile, the opposite will create $authen_url for the user grant access your applications to connect to their accounts.

- Create $authen_url link

  •  $state = '1102';
    $auth_url = 'https://www.fitbit.com/oauth2/authorize?' .
    'response_type=code' .
    '&client_id=' . $config['client_id'] .
    '&state=' . $state .
    '&redirect_uri=' . $config['redirect_uri'] .
    '&scope=profile';

    echo '<a href="' . $auth_url . '">Login with Fitbit</a>';

- Send request to get user's profile and print result on screen

  •  $access_token = $_SESSION[$config['session']];
    $endpoint = 'https://api.fitbit.com/1/user/-/profile.json';
    $header = array(
    'GET /1/user/-/profile.json HTTP/1.1',
    'Host: api.fitbit.com',
    'Authorization: Bearer ' . $access_token['access_token']
    );

    $data = projecthelper::make_request($endpoint, $header);
    var_dump($data);

<?php

if (!session_id()) {
session_start();
}
include './projecthelper.php';
$config = projecthelper::get_fitbit_config();
if (isset($_SESSION[$config['session']])) {
$access_token = $_SESSION[$config['session']];
$endpoint = 'https://api.fitbit.com/1/user/-/profile.json';
$header = array(
'GET /1/user/-/profile.json HTTP/1.1',
'Host: api.fitbit.com',
'Authorization: Bearer ' . $access_token['access_token']
);

$data = projecthelper::make_request($endpoint, $header);
var_dump($data);
} else {
$state = '1102';
$auth_url = 'https://www.fitbit.com/oauth2/authorize?' .
'response_type=code' .
'&client_id=' . $config['client_id'] .
'&state=' . $state .
'&redirect_uri=' . $config['redirect_uri'] .
'&scope=profile';

echo '<a href="' . $auth_url . '">Login with Fitbit</a>';
}

Create fitbit-callback.php

After granted access to your application, user will be redirected to your redirect_uri with authentication code. Then you will make request to get access_token.
(In some cases, your redirect_uri that you provide must match with the one you've described in app's settings, or redirect_uri must begin with https).

+ Check and extract authentication code and state

  • Send request to get access_token
  •  $code = $_GET['code'];
    $state = $_GET['state'];
    $endpoint = 'https://api.fitbit.com/oauth2/token';
    $header = array(
    'POST /oauth2/token HTTP/1.1',
    'Host api.fitbit.com',
    'Authorization: Basic ' . base64_encode($config['client_id'] . ':' . $config['client_secret']),
    'Content-Type: application/x-www-form-urlencoded'
    );
    $post_fields = 'grant_type=authorization_code' .
    '&client_id=' . $config['client_id'] .
    '&client_secret=' . $config['client_secret'] .
    '&code=' . $code .
    '&state=' . $state .
    '&redirect_uri=' . $config['redirect_uri'];
    $data = projecthelper::make_request($endpoint, $header, $post_fields);
  • Get result, if there is no error, save the result get into $_SESSION and navigate user to the login page.
  •  $_SESSION[$config['session']] = $data;
    header('Location: ' . $config['login']);
  • If there are errors, print to the screen
  •  print_r($data);

+ If there are errors, print to the screen

  • print_r($_GET);

<?php

if (!session_id()) {
session_start();
}
include './projecthelper.php';
$config = projecthelper::get_fitbit_config();

if (isset($_GET['code'])) {
$code = $_GET['code'];
$state = $_GET['state'];
$endpoint = 'https://api.fitbit.com/oauth2/token';
$header = array(
'POST /oauth2/token HTTP/1.1',
'Host api.fitbit.com',
'Authorization: Basic ' . base64_encode($config['client_id'] . ':' . $config['client_secret']),
'Content-Type: application/x-www-form-urlencoded'
);
$post_fields = 'grant_type=authorization_code' .
'&client_id=' . $config['client_id'] .
'&client_secret=' . $config['client_secret'] .
'&code=' . $code .
'&state=' . $state .
'&redirect_uri=' . $config['redirect_uri'];
$data = projecthelper::make_request($endpoint, $header, $post_fields);
if (isset($data['access_token'])) {
$_SESSION[$config['session']] = $data;
header("Location: " . $config['login']);
} else {
var_dump($data);
}
} else {
echo $_GET['error'];
}

After implement code, run project and view result or you can view our example here. If you have questions, don't hesitate, tell us