oauth2-fitbit icon indicating copy to clipboard operation
oauth2-fitbit copied to clipboard

Server script to pull fitbit datat daily.

Open ghost opened this issue 4 years ago • 4 comments
trafficstars

I am trying to run this in a server script. Far as I know I can do the auth stuff in the browser to get the auth code, then plug it into another script and it should work? But I get invalid auth code? Any advice? Thanks

ghost avatar Sep 02 '21 05:09 ghost

./import.php
invalid_grant - Authorization code invalid: xxxxxxxx Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process.

timrabbetts avatar Sep 02 '21 05:09 timrabbetts

solved it here is the code, need to build my own token

<?php

require __DIR__ . '/vendor/autoload.php';

use djchen\OAuth2\Client\Provider\Fitbit;
use League\OAuth2\Client\Token\AccessToken;

$provider = new Fitbit([
  'clientId'          => 'xxxxx',
  'clientSecret'      => 'xxxxx',
  'redirectUri'       => 'xxxxxxx'
]);

$existingAccessToken = new AccessToken([
  'access_token' => 'xxxxxx',
  'refresh_token' => 'xxxxx',
  'expires' => 1630589048
]);

if ($existingAccessToken->hasExpired()) {
  $newAccessToken = $provider->getAccessToken('refresh_token', [
    'refresh_token' => $existingAccessToken->getRefreshToken()
  ]);
}

$request = $provider->getAuthenticatedRequest(
  Fitbit::METHOD_GET,
  Fitbit::BASE_FITBIT_API_URL . '/1/user/-/foods/log/water/date/2021-07-01/2021-07-30.json',
  $existingAccessToken,
  ['headers' => [Fitbit::HEADER_ACCEPT_LANG => 'en_GB'], [Fitbit::HEADER_ACCEPT_LOCALE => 'en_US']]
);

$response = $provider->getParsedResponse($request);

print '<pre>';
print_r($response);

timrabbetts avatar Sep 02 '21 05:09 timrabbetts

I copied the access token and refresh token i got when I did the browser based auth.

timrabbetts avatar Sep 02 '21 05:09 timrabbetts

refresh token not working says invalid refresh token working on a fix for this

timrabbetts avatar Sep 04 '21 05:09 timrabbetts