SUUNTO API OAuth2 401 Unauthorized Response Error in Flutter

SUUNTO API OAuth2 401 Unauthorized Response Error in Flutter

  • Blog
  • 1 min read

If you are getting the "401 Unauthorized Response" error when integrating SUUNTO API in Flutter, follow these steps to resolve it.

Below is the example code which we usually use to follow to integrate SUUNTO API with flutter:

const REDIRECT_URL = "suuntoflutter://redirect/";

  Future _getSuuntoToken(
    String clientId,
    String secret,
    String code,
  ) async {
    final encodedRedirectUrl = Uri.encodeQueryComponent(REDIRECT_URL);
    final params = "?grant_type=authorization_code&code=$code&redirect_uri=$encodedRedirectUrl";
    final tokenRequestUrl = "https://cloudapi-oauth.suunto.com/oauth/token" + params;
    final base64String = base64.encode(utf8.encode("$clientId:$secret"));
    final tokenResponse = await http.post(Uri.parse(tokenRequestUrl), headers: {"Authorization": "Basic: $base64String"});

And the problem in the above code is the extra semicolon in the Basic Authentication header string. So now change the "Basic: $base64String" line as shown below:

final tokenResponse = await http.post(Uri.parse(tokenRequestUrl), headers: {"Authorization": "Basic $base64String"});

The above correct line of code should resolve the 401 Unauthorized Response error in Flutter.

Reference:

See also: