Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Followed the docs and doesn't work #95

Open
timrabbetts opened this issue Jun 18, 2021 · 13 comments
Open

Followed the docs and doesn't work #95

timrabbetts opened this issue Jun 18, 2021 · 13 comments

Comments

@timrabbetts
Copy link

timrabbetts commented Jun 18, 2021

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
Copy link
Owner

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

@namelivia namelivia changed the title Followed the docs and doesnt work Followed the docs and doesn't work Jun 18, 2021
@namelivia
Copy link
Owner

namelivia commented Jun 18, 2021

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!

@timrabbetts
Copy link
Author

timrabbetts commented Jun 19, 2021 via email

@timrabbetts
Copy link
Author

timrabbetts commented Jun 19, 2021

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
Copy link
Owner

namelivia commented Jun 19, 2021

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.

@timrabbetts
Copy link
Author

timrabbetts commented Jun 24, 2021

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);
}

@timrabbetts
Copy link
Author

timrabbetts commented Jun 24, 2021

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...)

@timrabbetts
Copy link
Author

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

@timrabbetts
Copy link
Author

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
Copy link
Owner

namelivia commented Jul 3, 2021

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
Copy link
Owner

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
Copy link
Contributor

Olindn commented Aug 8, 2021

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

@namelivia
Copy link
Owner

Ow! 🤦 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:

  • Clone that project
  • Copy .env.example to .env
  • Fill in FITBIT_CLIENT_ID, YOUR_FITBIT_CLIENT_SECRET and YOUR_FITBIT_REDIRECT_URL with your credentials. For the sake of this test the redirect URL is not that important so you can just point it to http://localhost
  • Execute ./run
  • Since the auth token is still not present the execution will finish with a message like Retrieve auth token from and a fitbit URL, open that URL in your browser.
  • Fitbit will ask for some permissions, and after that it will redirect your browser to YOUR_FITBIT_REDIRECT_URL with a url parameter like: ?code=920e8c350103e1be0a8a20d78bd491c90d56118f#_=_ the code is the alphanumeric string, it is important not to pick the # and what goes after, for this example it would be: 920e8c350103e1be0a8a20d78bd491c90d56118f.
  • Now open your .env file and paste this on FITBIT_AUTH_CODE
  • Execute ./run again and you would get: Success! Check the contents on php/output/result.
  • Open that file and see the data pulled from Fitbit.

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants