user2320607
user2320607

Reputation: 425

fitbit php auth doesn't work

After 10 hours of trying various fitbit php libraries I'm turning to stackoverflow for help.

This doesn't work: https://github.com/heyitspavel/fitbitphp

Using

 $profile = $fitbit->getProfile();

with that library returns

Fatal error: Uncaught exception 'FitBitException' with message 'Your Fitbit request failed. Code: 400' in /var/www/api/fitbitphp.php:324 Stack trace: #0 /var/www/api/addFitbit.php(22): FitBitPHP->getProfile() #1 {main} thrown in /var/www/api/fitbitphp.php on line 324

This the library on the fitbit website, seems like a lot of people have a problem with this.

    public function getProfile()
{
    $headers = $this->getHeaders();

    try {
        $this->oauth->fetch($this->baseApiUrl . "user/" . $this->userId . "/profile." . $this->responseFormat, null, OAUTH_HTTP_METHOD_GET, $headers);
    } catch (Exception $E) {
    }
    $response = $this->oauth->getLastResponse();
    $responseInfo = $this->oauth->getLastResponseInfo();
    if (!strcmp($responseInfo['http_code'], '200')) {
        $response = $this->parseResponse($response);

        if ($response)
            return $response;
        else
            throw new FitBitException($responseInfo['http_code'], 'Fitbit request failed. Code: ' . $responseInfo['http_code']);
    } else {
        throw new FitBitException($responseInfo['http_code'], 'Your Fitbit request failed. Code: ' . $responseInfo['http_code']);
    }
}

I tried this here as well but it doesn't return the user token or session id https://github.com/nostra999/fitbit-api

Upvotes: 2

Views: 419

Answers (1)

Todor.Angelov
Todor.Angelov

Reputation: 77

Perhaps missed out the init step, as described in the lib README file (https://github.com/heyitspavel/fitbitphp/blob/master/README.md)

Simple working usage is:

<?php
define('FITBIT_KEY', '777'); // The application key registered
define('FITBIT_SECRET', '777'); // The application secret registered

$fitbit = new FitBitPHP(FITBIT_KEY, FITBIT_SECRET);
$fitbit->initSession('http://localhost:8080/fibit'); // callback URL
$fitbit->getProfile();

Also from the Fitbit API Documentation: https://wiki.fitbit.com/display/API/API+Response+Format+And+Errors#APIResponseFormatAndErrors-Response

400 Bad Request Any case where either endpoint doesn't exist, resource path parameters are invalid, POST request parameters are invalid or no Authentication header provided. This doesn't include invalid specific resource ids

If this does not help, please provide the full code that you run, not just

$profile = $fitbit->getProfile();

Upvotes: 1

Related Questions