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

I want to log in to fitbit using Flutter and get permission to receive data.

ANSWERED

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;
}
}
}
Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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.

 

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google

View best answer in original post

Best Answer
0 Votes
7 REPLIES 7

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

  1. What is your "application type" set to in the registered application settings at https://dev.fitbit.com?
  2. 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.
  3. Please verify the client_id and redirect_uri parameter values are being passed through to the authorization URL correctly.
  4. What point in the authorization flow are you getting the error?   Are you seeing the consent page?  
  5. What step in your code is the error occurring?
  6. Would you provide the detailed message of your platformException error? 

Thanks!

  

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

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;
}
}
}
Best Answer
0 Votes

Hello

The above problem has been solved,
but I have been blocked from getting accessToken and refreshToken,
can you please let me know?

Thanks

Best Answer
0 Votes

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?

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

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.

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes

Hello @GordonFitbit 

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?

Best Answer
0 Votes

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.

 

Gordon Crenshaw
Senior Technical Solutions Consultant
Fitbit Partner Engineering & Web API Support | Google
Best Answer
0 Votes