namelivia / fitbit-http-php

PHP SDK for accessing the Fitbit HTTP API
MIT License
10 stars 4 forks source link

Followed the docs and doesn't work #95

Open ghost opened 3 years ago

ghost commented 3 years ago

Class 'Namelivia\Fitbit\Api\Api' not found

By the looks of things you have refactored the code and not updated the docs, I suspect I need to do this?

<?php

use Namelivia\Fitbit\ServiceProvider;

require 'vendor/autoload.php';

$client = ServiceProvider::build(
    '/tmp/token',
    'someClientId',
    'someClientSecret',
    'https://myapp.com/authorized'
);

But didn't work can you provide the correct code to get it working please? thanks

namelivia commented 3 years ago

Hello! I won't be surprised this is a bit unattended, let me take a look at it this weekend.

namelivia commented 3 years ago

Hi, I've tested the library myself and you are right, it wasn't working. As I mentioned it's a bit abandoned but I'll see if I can save some time to revamp it and fix the README file.

Still, you can make it run by updating to 0.0.14 (I just tagged this version a couple of hours ago). And then create an instance like:

use kamermans\OAuth2\Persistence\NullTokenPersistence;
use Namelivia\Fitbit\ServiceProvider;

$fitbit = ServiceProvider::build(
    new NullTokenPersistence(),
    'someClientId',
    'someClientSecret',
    'https://myapp.com/authorized'
);

The first parameter is a token persistence from https://github.com/kamermans/guzzle-oauth2-subscriber. In the example I'm passing NullTokenPersistence but you can pass any of them.

Then you could just query data, but you'll receive a missing auth code exception, that's because you have to first visit the auth code URL on Fitbit and login, and get the code from there.

To get the URL you can just:

$fitbit->getAuthUri()

And pass the code you get from there to the instance like:

$fitbit->setAuthorizationCode("TheAuthorizationCodeYouJustGot")

Then you could just query data like:

$activities = $fitbit->activities()->favorites()->get();

Hope this helps!

ghost commented 3 years ago

Thanks thats great!

On Fri, Jun 18, 2021 at 11:17 PM José Ignacio Amelivia Santiago < @.***> wrote:

Hi, I've tested the library myself and you are right, it wasn't working currently. As I said its a bit abandoned but I'll see if I can save some time to revamp it.

Still, you can make it run by updating to 0.0.14 (I just tagged this version a couple of hours ago). And then create an instance like:

use kamermans\OAuth2\Persistence\NullTokenPersistence;use Namelivia\Fitbit\ServiceProvider; $fitbit = ServiceProvider::build( new NullTokenPersistence(), 'someClientId', 'someClientSecret', 'https://myapp.com/authorized' );

The first parameter is a token persistence from https://github.com/kamermans/guzzle-oauth2-subscriber. In the example I'm passing NullTokenPersistence but you can pass any of them https://github.com/kamermans/guzzle-oauth2-subscriber#access-token-persistence .

Then you could just query data, but you'll receive a missing auth code exception, that's because you have to first visit the auth code URL on Fitbit and login, and get the code from there.

To get the URL you can just:

$fitbit->getAuthUri()

And pass the code you get from there to the instance like:

$fitbit->setAuthorizationCode("TheAuthorizationCodeYouJustGot")

Then you could just query data like:

$activities = $fitbit->activities()->favorites()->get();

Hope this helps!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/namelivia/fitbit-http-php/issues/95#issuecomment-864299641, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC4QDQEFI7YYV7X6K5Z67RDTTPAXTANCNFSM465DRNQA .

ghost commented 3 years ago

Tried this code and didnt work, can you give me a complete example please?

<?php

require 'vendor/autoload.php';

use kamermans\OAuth2\Persistence\NullTokenPersistence;
use Namelivia\Fitbit\ServiceProvider;

$fitbit = ServiceProvider::build(
    new NullTokenPersistence(),
    '####',
    '####',
    '####'
);

$fitbit->getAuthUri();
$fitbit->setAuthorizationCode("TheAuthorizationCodeYouJustGot")
$activities = $fitbit->activities()->favorites()->get();
namelivia commented 3 years ago

Sure, could I get more information about the error you got?

$fitbit->getAuthUri();

returns a URL, you need to actually visit that URL and login there in order to get the code you need to pass to:

$fitbit->setAuthorizationCode()

as a parameter. Note that in order to get a valid url and not end up in the Fitbit's 404 page the clientId must be valid, so you must first get one by creating an account there.

ghost commented 3 years ago

here is my current code, I am trying to run it from command line not in browser FYI.


<?php

require 'vendor/autoload.php';

use kamermans\OAuth2\Persistence\NullTokenPersistence;
use Namelivia\Fitbit\ServiceProvider;

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

// $_ENV defo contains correct values!

$fitbit = ServiceProvider::build(
    new NullTokenPersistence(),
    $_ENV['client_id'],
    $_ENV['client_secret'],
    $_ENV['redirect_url']
);

if (empty($_ENV['auth_code'])) {

    print 'GO TO THIS URL AND GET THE code=xxxx value!!!';
    print $fitbit->getAuthUri();
    die();

} else {

    $code = $_ENV['auth_code'];
    $fitbit->setAuthorizationCode($code);
    $activities = $fitbit->activities()->favorites()->get();
    print_r($activities);
}
ghost commented 3 years ago

First time it makes me go to URL where I get Invalid state but I do get a code=xxx in the URL so I take the code and add it to my env file. Then I run it again.

PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.fitbit.com/oauth2/token resulted in a 400 Bad Request response: {"errors":[{"errorType":"invalid_grant","message":"Authorization code invalid: xxxxxxxxx# (truncated...)

ghost commented 3 years ago

I really appreciate the help, I've tried doing OAuth in PHP on its own and there is a PHP lib on fitbit but no joy at all. thanks

ghost commented 3 years ago

What I am trying to do is build a PHP script to pull my daily data for water, food and weight, and store it in a db, then build a realtime dashboard.

namelivia commented 3 years ago

Hey @timrabbetts yes, it makes sense, I started building this project with something similar in mind. That looks like a problem with the authorization code you picked from the URL, I can recommend you trying it again and paying attention to the auth code limits, if I recall right the code displayed on the URL may have some trailing chars that had to be removed, but I'm speaking off the top of my head. The code you posted is valid and should already work.

namelivia commented 3 years ago

Also, I recommend you trying this project, which contains an example using the Laravel framework of something similar to what you are trying to achieve, it uses the same code we're speaking about here.

Olindn commented 3 years ago

@namelivia I might be overlooking but that repo seems to be an "empty" Laravel project

namelivia commented 3 years ago

Ow! :facepalm: Sorry for that, I thought I finished that back then, I'll delete that repo since it's only creating confusion. Check this other repository: https://github.com/namelivia/fitbit-api-probe

I use this to know that the library works, it requires docker and docker-compose so you don't need to alter your dev environment to run it.

The instructions are:

The script this test is running is here

Could you please test it and see if it works for you?

Sorry for the half abandonment of the project, since now this project didn't raise much interest and also I don't own a Fitbit device anymore. But if I see people interested in it I may spend some time polishing it.