Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Access token is missing. Please authenticate with Fitbit first.

ANSWERED
<?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',
            'redirect_uri' => 'https://uat.onehealthassist.com/',
            'response_type' => 'code',
            'scope' => 'activity heartrate location profile ' . // Add additional scopes here
                        'nutrition weight sleep social settings',
        ]);

        return redirect('https://www.fitbit.com/oauth2/authorize?' . $query);
    }

    public function handleFitbitCallback(Request $request)
{
    $response = Http::asForm()->post('https://api.fitbit.com/oauth2/token', [
        'client_id' => '23RYH8',
        'client_secret' => '4b4ab96ad28ffa2843c69fb7c3513fdb',
        'code' => $request->code,
        'grant_type' => 'authorization_code',
        'redirect_uri' => 'https://uat.onehealthassist.com/',
    ]);

    $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([
            'base_uri' => 'https://api.fitbit.com/',
        ]);
    }

    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 
Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

give proper header format so i can use in laravel this is the function name handleFitbitCallback

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

in this code i am getting error in callback function 

"errorType" => "invalid_client"
"message" => "Invalid authorization header format. The header was not recognized to be
a valid header for any of known implementations or a client_id was not specified in case of
a public client Received header = null. Visit https://dev.fitbit.com/docs/oauth2 for more information
on the Fitbit Web API authorization process

 this so please let me know to resolve this issue in laravel 

Best Answer
0 Votes

give proper header format so i can use in laravel this is the function name handleFitbitCallback

Best Answer
0 Votes