<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Services\FitbitService;
use Illuminate\Support\Facades\Log;
class FitbitController extends Controller
{
protected $fitbitService;
public function __construct(FitbitService $fitbitService)
{
$this->fitbitService = $fitbitService;
}
public function redirectToFitbit()
{
$query = http_build_query([
'client_id' => '23RYH8',
'response_type' => 'code',
'scope' => 'activity heartrate location profile ' . // Add additional scopes here
'nutrition weight sleep social settings',
]);
}
public function handleFitbitCallback(Request $request)
{
'client_id' => '23RYH8',
'client_secret' => '4b4ab96ad28ffa2843c69fb7c3513fdb',
'code' => $request->code,
'grant_type' => 'authorization_code',
]);
$accessToken = null;
// Check if the response contains the access token
if ($response->json() && array_key_exists('access_token', $response->json())) {
$accessToken = $response->json()['access_token'];
} else {
// Log an error or handle the absence of access token as needed
Log::error('Access token not found in Fitbit API response.');
// You may also want to redirect with an error message
return redirect('/fitbit/dashboard')->with('error', 'Access token not found. Please try again later.');
}
// Redirect to dashboard with access token as query parameter
return redirect('/fitbit/dashboard?access_token=' . $accessToken);
}
public function dashboard(Request $request)
{
try {
// Get the access token from the request query parameters
$accessToken = $request->query('access_token');
// Check if access token is missing
if (!$accessToken) {
// Provide an error message for missing access token
$errorMessage = 'Access token is missing. Please authenticate with Fitbit first.';
return view('fitbit.dashboard', compact('errorMessage'));
}
// Fetch Fitbit data using access token
$fitbitData = $this->fitbitService->getFitbitData($accessToken);
return view('fitbit.dashboard', compact('fitbitData'));
} catch (\Exception $e) {
// Log the error
Log::error('Dashboard Error: ' . $e->getMessage());
// Provide a generic error message to the users
return view('fitbit.dashboard')->with('error', 'An error occurred while fetching Fitbit data. Please try again later.');
}
}
} i have made this controller for fitbit api but it is getting error in dashboard Access token is missing. Please authenticate with Fitbit first. i have done the also
in server also
<?php
namespace App\Services;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Log;
class FitbitService
{
protected $client;
public function __construct()
{
$this->client = new Client([
]);
}
public function getFitbitData($accessToken)
{
try {
$response = $this->client->request('GET', '1/user/-/profile.json', [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
return json_decode($response->getBody()->getContents(), true);
} catch (ClientException $e) {
// Handle client errors (e.g., 401 Unauthorized)
Log::error('Fitbit API Client Error: ' . $e->getMessage());
throw $e; // Rethrow the exception for further handling
} catch (\Exception $e) {
// Handle other exceptions
Log::error('Fitbit API Error: ' . $e->getMessage());
throw $e; // Rethrow the exception for further handling
}
}
} added this but then also not getting data give me the solution for this why authentication is not happening give me proper solution for this