02-21-2018 15:29
02-21-2018 15:29
Hi,
I'm following this tutorial:
https://dev.fitbit.com/apps/oauthinteractivetutorial
I've installed Fitbit Provider for OAuth 2.0 Client (https://github.com/djchen/oauth2-fitbit) using Composer
OAuth 2.0 Application Type is set to Client
My callback URL is set to https://www.edge-trainer.com/fitbit/djchen-test.php in the Fitbit dev console as well as within https://www.edge-trainer.com/fitbit/djchen-test.php.
I get a page error stating "Invalid state" after clicking the link below:
This is the contents of djchen-test.php:
<?php require_once 'vendor/autoload.php'; use djchen\OAuth2\Client\Provider\Fitbit; $provider = new Fitbit([ 'clientId' => '22CMHC', 'clientSecret' => 'c4116719a2e03d8f0a88a880456e73b4', 'redirectUri' => 'https://www.edge-trainer.com/fitbit/djchen-test.php' ]); // 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. $_SESSION['oauth2state'] = $provider->getState(); // Redirect the user to the authorization URL. header('Location: ' . $authorizationUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || array_key_exists('oauth2state', $_SESSION) && ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { try { // Try to get an access token using the authorization code grant. $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // We have an access token, which we may use in authenticated // requests against the service provider's API. echo $accessToken->getToken() . "\n"; echo $accessToken->getRefreshToken() . "\n"; echo $accessToken->getExpires() . "\n"; echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n"; // Using the access token, we may look up details about the // resource owner. $resourceOwner = $provider->getResourceOwner($accessToken); var_export($resourceOwner->toArray()); // 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( Fitbit::METHOD_GET, Fitbit::BASE_FITBIT_API_URL . '/1/user/-/profile.json', $accessToken, ['headers' => [Fitbit::HEADER_ACCEPT_LANG => 'en_US'], [Fitbit::HEADER_ACCEPT_LOCALE => 'en_US']] // Fitbit uses the Accept-Language for setting the unit system used // and setting Accept-Locale will return a translated response if available. // https://dev.fitbit.com/docs/basics/#localization ); // Make the authenticated API request and get the parsed response. $response = $provider->getParsedResponse($request); // If you would like to get the response headers in addition to the response body, use: //$response = $provider->getResponse($request); //$headers = $response->getHeaders(); //$parsedResponse = $provider->parseResponse($response); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token or user details. exit($e->getMessage()); } }
I've been spending weeks on this with no progress. Any help would be much appreciated!
Thanks,
Tim