03-19-2024 00:44
03-19-2024 00:44
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.
03-21-2024 06:13
03-21-2024 06:13
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.
03-19-2024 06:24
03-19-2024 06:24
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
Thanks!
03-19-2024 23:05
03-19-2024 23:05
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
03-19-2024 23:07
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
03-20-2024 07:21
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?
03-20-2024 07:27
03-20-2024 07:27
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.
03-20-2024 21:36
03-20-2024 21:36
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
03-21-2024 06:13
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.