03-19-2024 00:44
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-19-2024 00:44
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I'm trying to log in to fitbit using Flutter and get permission to get and upload data,
but platformexception is happening. Please help
Future<void> authFitbit() async {
final storage = FlutterSecureStorage();
try {
final result = await FlutterWebAuth.authenticate(
url: "https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=$clientId&redirect_uri=$redirectUri&scope=activity&expires_in=604800",
callbackUrlScheme: callbackUrlScheme,
);
var accessToken = Uri.parse(result)
.fragment
.split('&')
.firstWhere((element) => element.startsWith('access_token='))
.split('=')[1];
var refreshToken = Uri.parse(result)
.fragment
.split('&')
.firstWhere((element) => element.startsWith('refresh_token='))
.split('=')[1];
await storage.write(key: 'accessToken', value: accessToken);
await storage.write(key: 'refreshToken', value: refreshToken);
print("accessToken and refreshToken: $accessToken $refreshToken");
} catch (e) {
if (e is PlatformException && e.code == 'CANCELED') {
print('User canceled login');
Fluttertoast.showToast(msg: 'User canceled login');
} else {
rethrow;
}
}
}
Answered! Go to the Best Answer.
Accepted Solutions
03-21-2024 06:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



03-21-2024 06:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @whwndud
That's good news. We describe the application types in the documentation at https://dev.fitbit.com/build/reference/web-api/developer-guide/application-design/#Application-Types.
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

03-19-2024 06:24
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



03-19-2024 06:24
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @whwndud
I'm not familiar with Flutter, so maybe someone in the community who is can help on that piece. But, a couple of things to help debug the problem
- What is your "application type" set to in the registered application settings at https://dev.fitbit.com?
- If you're using the Authorization Code Grant Flow, you need to remove "expires_in=604800" from your authorization string. This parameter value is not configurable.
- Please verify the client_id and redirect_uri parameter values are being passed through to the authorization URL correctly.
- What point in the authorization flow are you getting the error? Are you seeing the consent page?
- What step in your code is the error occurring?
- Would you provide the detailed message of your platformException error?
Thanks!
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

03-19-2024 23:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-19-2024 23:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hello.
I'm currently working on getting Fitibit data using Flutter,
can you tell me how to get accessToken and refreshToken
Future<void> authFitbit() async {
final storage = FlutterSecureStorage();
try {
final result = await FlutterWebAuth.authenticate(
url: "https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=$clientId&redirect_uri=$redirectUri&scope=activity",
callbackUrlScheme: callbackUrlScheme);
final accessToken = Uri.parse(result).queryParameters['access_token'];
final refreshToken = Uri.parse(result).queryParameters['refresh_token'];
await storage.write(key: 'accessToken', value: accessToken);
await storage.write(key: 'refreshToken', value: refreshToken);
print("accessToken and refreshToken: $accessToken $refreshToken");
} catch (e) {
if (e is PlatformException && e.code == 'CANCELED') {
print(e);
Fluttertoast.showToast(msg: 'User canceled login $e');
} else {
rethrow;
}
}
}

03-19-2024 23:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-19-2024 23:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hello
The above problem has been solved,
but I have been blocked from getting accessToken and refreshToken,
can you please let me know?
Thanks

03-20-2024 07:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



03-20-2024 07:21
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @whwndud
What do you mean you are blocked from getting the access token and refresh token? Are you getting an error when exchanging the authorization code for the access token and refresh token?
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

03-20-2024 07:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



03-20-2024 07:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @whwndud
To prevent answering your question on 2 separate threads, I merged your retrieving access and refresh token question to your original post. Looking at your code, it appears you are missing a step. After the authorization page is displayed and the user consents, they will be redirected to your callbackUrl. The callbackUrl will contain an authorization code. You will need to parse the authorization code from the callback URL and use it in the /oauth2/token endpoint to get the access token and refresh token. See /oauth2/token. The authorization flow step can be found in the Developer's Guide.
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

03-20-2024 21:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-20-2024 21:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hello @Gordon-C
I solved the token problem and the login problem. Thank you
Additionally, when registering an app on fitbit dev,
there are 3 types in the OAuth 2.0 Application Type,
can you tell me what the difference is?

03-21-2024 06:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



03-21-2024 06:13
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi @whwndud
That's good news. We describe the application types in the documentation at https://dev.fitbit.com/build/reference/web-api/developer-guide/application-design/#Application-Types.
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

