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

getting error in authentication

ANSWERED

hi an issue when i run this link 
https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=23RYH8&redirect_uri=http://loca... 

it getting all permission page allow after allowing its redirect to localhost page not getting code 

in localhost getting error 

{
    "errors": [
        {
            "errorType": "invalid_grant",
            "message": "Authorization code invalid: 862ca91d7ed8b9b75142ae584899d69cb50bbe09 Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."
        }
    ],
    "success": false
}  how to resolve this issue let me know give me steps for this issue 

 

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Hi @phenil,

Are you exchanging the authorization code within 10 minutes of obtaining it? Auth codes have a default active duration of only 10 minutes and can only be used once. If you've attempted to use the code after 10 minutes or attempted to use it more than once, you may encounter the error you are reporting.

View best answer in original post

Best Answer
0 Votes
2 REPLIES 2

Hi @phenil,

Are you exchanging the authorization code within 10 minutes of obtaining it? Auth codes have a default active duration of only 10 minutes and can only be used once. If you've attempted to use the code after 10 minutes or attempted to use it more than once, you may encounter the error you are reporting.

Best Answer
0 Votes
 public function redirectToFitbit()
    {
        $query = http_build_query([
            'client_id' => '23RYH8',
            'redirect_uri' => 'https://uat.onehealthassist.com/fitbit/callback',
            'response_type' => 'code',
            'scope' => 'activity heartrate location profile ' . // Add additional scopes here
                'nutrition weight sleep social settings',
        ]);
            // Generate a unique code
        $code = uniqid();

        // Store the code in the session
        session(['fitbit_auth_code' => $code]);

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

    public function handleFitbitCallback(Request $request)
    {

        $code = session('fitbit_auth_code');
        dd($code);

        // $code = $request->query('code'); // Retrieve the 'code' parameter from the query string
       
        // Prepare the form data
        $formData = [
            'code' => $code, // Use the 'code' parameter retrieved from the request
            'grant_type' => 'authorization_code',
            'redirect_uri' => 'https://uat.onehealthassist.com/'
        ];

        $response = Http::withHeaders([
            'Authorization' => 'Basic MjNSWUg4OjRiNGFiOTZhZDI4ZmZhMjg0M2M2OWZiN2MzNTEzZmRi',
            'Content-Type' => 'application/x-www-form-urlencoded'
        ])->asForm()->post('https://api.fitbit.com/oauth2/token', $formData);

        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.');
        }
    } i am sending this but in the function 
handleFitbitCallback the code is getting null how can i pass this code i am getting url https://uat.onehealthassist.com/fitbit/callback?code=188ef0b7134beca6916f11777b85c339d3b3eee0#_=_ this way i am getting code how to pass this code let me know you have any idea about this 
Best Answer
0 Votes