Giter VIP home page Giter VIP logo

fitbitphp's Introduction

FitbitPHP

Basic wrapper for the OAuth-based FitBit REST API. See dev.fitbit.com for details on their OAuth implementation.

Both this library and the Fitbit API are in beta.

This library does not require the PHP OAuth extension. It should work on any server with PHP >= 5.3.

Installation

This package is installable with composer: "popthestack/fitbit": "dev-master"

Usage

You need a consumer key and secret. You can obtain them by registering an application at http://dev.fitbit.com.

Simple, but full OAuth workflow example:

$factory = new \Fitbit\ApiGatewayFactory;
$factory->setCallbackURL($callback_url);
$factory->setCredentials($consumer_key, $consumer_secret);

$adapter = new \OAuth\Common\Storage\Session();
$factory->setStorageAdapter($adapter);

$auth_gateway = $factory->getAuthenticationGateway();

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
    $auth_gateway->authenticateUser($_GET['oauth_token'], $_GET['oauth_verifier']);
} elseif (isset($_GET['connect'])) {
    $auth_gateway->initiateLogin();
}

if ($auth_gateway->isAuthorized()) {
    $user_gateway = $factory->getUserGateway();
    $user_profile = $user_gateway->getProfile();
    echo '<pre>';
    print_r($user_profile);
    echo '</pre>';
} else {
    echo 'Not connected.';
}

If you want to retrieve the OAuth token and secret from the session to store elsewhere (e.g. a database):

$storage = $factory->getStorageAdapter();
$token   = $storage->retrieveAccessToken('FitBit');

// Save these somewhere:
$oauth_token  = $token->getRequestToken();
$oauth_secret = $token->getRequestTokenSecret();

Here's how to use your OAuth token and secret without the Session storage adapter. It's a little cumbersome, but it works. If I ever have time for it, I'd like to replace the current OAuth library with something that doesn't enforce so much... stuff.

$token = new \OAuth\OAuth1\Token\StdOAuth1Token();
$token->setRequestToken($oauth_token);
$token->setRequestTokenSecret($oauth_secret);
$token->setAccessToken($oauth_token);
$token->setAccessTokenSecret($oauth_secret);

$adapter = new \OAuth\Common\Storage\Memory();
$adapter->storeAccessToken('FitBit', $token);

$factory->setStorageAdapter($adapter);

$user_gateway = $factory->getUserGateway();
$food_gateway = $factory->getFoodGateway();

$user_profile = $user_gateway->getProfile();
$user_devices = $user_gateway->getDevices();
$foods        = $food_gateway->searchFoods('banana split');

echo '<pre>';
print_r($user_profile);
print_r($user_devices);
print_r($foods);
echo '</pre>';

Notes

  • By default, all requests assume you want data for the authorized user (viewer). There are, however, several endpoints you can use to access the data of other Fitbit users, given that you have permission to access their data. This is accomplished by setting the Fitbit User ID with the setUserID method available on ApiGatewayFactory and the Endpoint Gateways (e.g. UserGateway, FoodGateway).
  • Subscriptions: this library has some basic methods to add/delete subscriptions, but it's your responsibility to track the list and maintain server endpoints to receive notifications from Fitbit, as well as register them at http://dev.fitbit.com. See Subscriptions API for more information.

fitbitphp's People

Contributors

jgkim avatar pavelrisenberg avatar popthestack avatar saschahagedorn avatar thesavior avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

fitbitphp's Issues

SetUserId

Where do you change what user you want to request information from? I am able to pull mine only when logging in, any help would be appreciated

Getting "Token not found in session, are you sure you stored it?" when trying to access data using consumer key only authorization

I've been trying to get the sleep data of my test user using the consumer key only verification.

So I've set the privacy setting of "Sleep" to "Anyone"

Here's my code to get the sleep data:

    $factory = new \Fitbit\ApiGatewayFactory();
    $factory->setCallbackURL($connection['callbackUrl']);
    $factory->setCredentials($connection['consumerKey'],   $connection['consumerSecret']);

    // Declared this to avoid the "missing storage adapter" error.
    $adapter = new \OAuth\Common\Storage\Session();
    $factory->setStorageAdapter($adapter);

    $sleepGateway = $factory->getSleepGateway();
    $sleepGateway->setUserID('XXXXX');
    print_r($sleepGateway->getSleep((new \DateTime('20140409'))));

However, whenever I try to access either the sleep data or the profile given that they are in Anyone setting returns "Token not found in session, are you sure you stored it?".

Any help on this issue would be appreciated. Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.