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

invalid_request

 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)
    {
        $code = $request->query('code'); // Retrieve the 'code' parameter from the query string

        // if (!$code) {
        //     // Handle error: 'code' parameter is missing
        //     return redirect('/fitbit/dashboard')->with('error', 'Authorization code missing. Please try again.');
        // }

        $response = Http::withHeaders([
            'Authorization' => 'Basic MjNSWUg4OjRiNGFiOTZhZDI4ZmZhMjg0M2M2OWZiN2MzNTEzZmRi',
            'Content-Type' => 'application/x-www-form-urlencoded',
            'client_id' => '23RYH8',
            'client_secret' => '4b4ab96ad28ffa2843c69fb7c3513fdb',
            'code' => $code, // Use the 'code' parameter retrieved from the request
            'grant_type' => 'authorization_code',
            'redirect_uri' => 'https://uat.onehealthassist.com/',
        ])->asForm()->post('https://api.fitbit.com/oauth2/token', [
           
        ]);

        dd($response->json());
        // Check if the response is successful and contains the access token
        if ($response->successful() && $response->json() && array_key_exists('access_token', $response->json())) {
            $accessToken = $response->json()['access_token'];

            // Redirect to dashboard with access token as query parameter
            return redirect('/fitbit/dashboard?access_token=' . $accessToken);
        } 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.');
        }
    }

    public function dashboard(Request $request)
    {
        // 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'));
        }

        // You can now use $accessToken to make authenticated requests to the Fitbit API
        // Example: Fetch Fitbit data using access token

        return view('fitbit.dashboard', compact('accessToken'));
    } 
i have added all headers properly then also getting error in this function 
handleFitbitCallback getting error 

"errorType" => "invalid_request"
"message" => "Missing 'grant_type' parameter value. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."

i had given grant_type but then also it is not going futher for authentication and fetching data of this give me the solution of this issue 

 

Best Answer
0 Votes
0 REPLIES 0