예제 #1
0
<?php

namespace Hello;

require __DIR__ . '/../vendor/autoload.php';
use djchen\OAuth2\Client\Provider\Fitbit;
// Constant clientId
const CLIENT_ID = '227HB2';
// Constant Client Secret
const CLIENT_SECRET = 'b62af360ee95e6f8ab519f16f7a9fdef';
// Constant redirect URI
const REDIRECT_URI = 'http://localhost/beat_mvp/cec205/src/hello.php';
$token = "";
$provider = new Fitbit(['clientId' => CLIENT_ID, 'clientSecret' => CLIENT_SECRET, 'redirectUri' => REDIRECT_URI]);
$existingAccessToken = $token;
if ($existingAccessToken != "") {
    if ($existingAccessToken != "" || $existingAccessToken->hasExpired()) {
        $newAccessToken = $provider->getAccessToken('refresh_token', ['refresh_token' => $existingAccessToken->getRefreshToken()]);
        // Reset the token
        $token = $newAccessToken;
    }
}
// start the session
session_start();
// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {
    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();
    // Get the state generated for you and store it to the session.
예제 #2
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use djchen\OAuth2\Client\Provider\Fitbit;
// Constant clientId
const CLIENT_ID = '227HB2';
// Constant Client Secret
const CLIENT_SECRET = 'b62af360ee95e6f8ab519f16f7a9fdef';
// Constant redirect URI
const REDIRECT_URI = 'http://localhost/beat_mvp/cec205/src/hello.php';
$accessToken = "";
if (isset($_GET['accessToken'])) {
    $accessToken = $_GET['accessToken'];
    unset($_GET['accessToken']);
}
$provider = new Fitbit(['clientId' => CLIENT_ID, 'clientSecret' => CLIENT_SECRET, 'redirectUri' => REDIRECT_URI]);
// The provider provides a way to get an authenticated API request for
// the service, using the access token; it returns an object conforming
// to Psr\Http\Message\RequestInterface.
$request = $provider->getAuthenticatedRequest('GET', 'https://api.fitbit.com/1/user/-/profile.json', $accessToken);
// Make the authenticated API request and get the response.
$userData = $provider->getResponse($request);
$name = $userData['user']['displayName'];
$gender = $userData['user']['gender'];
$heightInches = round($userData['user']['height'] * 0.393701);
$weight = intval($userData['user']['weight'] * 2.20462);
$age = $userData['user']['age'];
$avatar = $userData['user']['avatar150'];
$heightString = intval($heightInches / 12) . '\' ' . $heightInches % 12 . '"';
$bmi = intval($weight / ($heightInches * $heightInches) * 703);
?>
예제 #3
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use djchen\OAuth2\Client\Provider\Fitbit;
// Constant clientId
const CLIENT_ID = '227HB2';
// Constant Client Secret
const CLIENT_SECRET = 'b62af360ee95e6f8ab519f16f7a9fdef';
// Constant redirect URI
const REDIRECT_URI = 'http://localhost/beat_mvp/cec205/src/hello.php';
$accessToken = "";
if (isset($_GET['accessToken'])) {
    $accessToken = $_GET['accessToken'];
    unset($_GET['accessToken']);
}
$provider = new Fitbit(['clientId' => CLIENT_ID, 'clientSecret' => CLIENT_SECRET, 'redirectUri' => REDIRECT_URI]);
// The provider provides a way to get an authenticated API request for
// the service, using the access token; it returns an object conforming
// to Psr\Http\Message\RequestInterface.
$request = $provider->getAuthenticatedRequest('GET', 'https://api.fitbit.com/1/user/-/profile.json', $accessToken);
// Make the authenticated API request and get the response.
$userData = $provider->getResponse($request);
//var_dump($userData);
echo "\n\n";
$date = date("Y-m-d");
$request = $provider->getAuthenticatedRequest('GET', "https://api.fitbit.com/1/user/-/activities/date/{$date}.json", $accessToken);
// Make the authenticated API request and get the response.
$activity = $provider->getResponse($request);
//var_dump($activity);
echo "\n\n";
$request = $provider->getAuthenticatedRequest('GET', "https://api.fitbit.com/1/user/-/activities/steps/date/today/1m.json", $accessToken);