getTokens method

Future<QuickbooksOauth2Tokens> getTokens({
  1. required String authorizationCode,
  2. String? realmId,
})

Gets the QuickbooksOauth2Tokens for a user with his authorizationCode

Implementation

Future<QuickbooksOauth2Tokens> getTokens({
  required String authorizationCode,
  String? realmId,
}) async {
  if (realmId == "null") {
    realmId = null;
  }
  //Gets the config values
  var config =
      await _configService.getConfiguration(isProduction: _isProduction);

  //Sets the oauth connection
  var grant = oauth2.AuthorizationCodeGrant(
      _clientId,
      Uri.parse(config.authorizationEndpoint),
      Uri.parse(config.tokenEndpoint),
      secret: _clientSecret);
  var scopes = Oauth2Scopes.values;
  var scopesStrings =
      Oauth2ScopesConvertExtension.generateScopeStrings(scopes: scopes);
  grant.getAuthorizationUrl(Uri.parse(_redirectUrl),
      scopes: scopesStrings, state: _state);

  // Gets the first tokens
  var client = await grant.handleAuthorizationResponse({
    "code": authorizationCode,
    "state": _state,
    "realmid": realmId.toString(),
  });

  // Refreshes the tokens to get all needed information
  var tokens = await refreshTokens(
      refreshToken: client.credentials.refreshToken ?? "");
  tokens.companyId = realmId;
  return tokens;
}