Fitbit Api "Missing 'grant_type' parameter value" issue

Hey I am trying to get Fitbit API oauth2/token access but its throwing the following error

There was an issue setting up your call.

Raw response for the API 
Status code 400
{"errors":[{"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."}],"success":false}

Here are the screenshots
1-

2-

3-

I am properly passing all the parameters and headers but still showing this. I tried to use various variations but no success.

Here is the Fitbit API documentation

I tried to add the grant_type parameter in call then it shows the following error

In this case, it shows Expecting: application/x-www-form-urlencoded. I tried to look on forum regarding this but couldn’t find any proper solution.

Need help to solve this. If someone has implemented this call or knows the solution. I will be thankful.

Regards
Muhammad Ammar

This may just be me but 9/10 times I create the oAuth flow manually instead of using bubbles built in oauth2 feature. It very rarely works as you want in my experience and most APIs I’ve needed to do it on (Google, ticktock, outlook, etc) all tend to work better when done manually.

Manually: button click to go to auth URL. Watching redirect URL for code or token, saving that and storing refresh times and refreshing when necessary.

I have exactly same issue.
Did you solve this problem?

Can you share screenshot? The original one have some issues related to use of auth + use of manual auth. This is not compatible. + Authorize url was not correct.

Thank you for your response!

I’ve got the “code” for token by using “open an external website”, then call the API to get the token with that “code”, however, the response is always “Missing ‘grant_type’”.

I’ve tried the same parameter on FitBit OAuth2 Tutorial (OAuth 2.0 Tutorial) and it worked there.

You have set body type to form data. Change for raw and in body set
grant_type=authorization_code&redirect_url=…

I’ve changed it to raw body, however, now it says “Authorization code invalid” although I’m sure code is correct.

I’ve attached screenshot and FitBit Tutorial curl, which works.

My raw body is not correct??

Code doesn’t last long. Clear what you have, request a new code, and go fast to copy and paste it to APi Connector and initialize it

Thank you so much. I’ve tried as fast as I can, but still I could not succeed.

I could use the same code in FitBit Tutorial curl to receive the correct Token after I failed it on the Bubble API connector, so I could not figure out why Bubble API does not work.

Instead, I made a backend server side action just to get the token and it worked.
Thank you for your support.

Reference of my plugin server-side action code for anyone having the same issue
(FYI: I was trying to connect Fitbit account to the existing users who did not use Fitbit account to signup. If you want user to signup/login with Fitbit account, I guess Fitbit plugin will be a easier solution.)
:

async function(properties, context) {
    const axios = require('axios');
    const base64 = require('base-64');

    // Extract properties
    const redirectUri = properties.redirect_uri;
    const code = properties.code;

    // Retrieve Fitbit API credentials from context keys
    const clientId = context.keys['FitBit Client ID'];
    const clientSecret = context.keys['FitBit Client Secret'];
    const authHeader = base64.encode(`${clientId}:${clientSecret}`);

    // Prepare the request payload
    const payload = new URLSearchParams();
    payload.append('grant_type', 'authorization_code');
    payload.append('redirect_uri', redirectUri);
    payload.append('code', code);

    try {
        // Make the request to Fitbit API using axios
        const response = await axios.post('https://api.fitbit.com/oauth2/token', payload.toString(), {
            headers: {
                'Authorization': `Basic ${authHeader}`,
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        });

        // Return the required tokens and user ID with a success result
        return {
            result: true,
            access_token: response.data.access_token,
            refresh_token: response.data.refresh_token,
            user_id: response.data.user_id
        };
    } catch (error) {
        // Log the error details for debugging
        console.error('Error fetching Fitbit token:', error.response ? error.response.data : error.message);

        // Handle errors and return a failure result
        return {
            result: false,
            error: error.response ? error.response.data : error.message
        };
    }
}