Connect Flutter with Oracle Database via OAuth2 authorized JSON-API

A few month ago I started with coding in Dart/Flutter. One of my first „projects“ was to create a mobile app for my pantry inventory. To have the data available on different devices synchronized it was needed to save it somewhere in the cloud.

Pantry mobile app
Pantry Mobile App

Of course, there a several options available to save data in the cloud. Some of them are not for free, some will not follow my point of view with GDPR regulations. The good thing was, that Oracle offers an always free database in the cloud. And since I’m working with Oracle APEX the whole setup was available for me without starting from scratch.

In this always free database, Oracle offers an autonomous database with a REST interface and you can decide in which location it should running. There are a lot more options available (Oracle APEX, VM’s, …) but these are not needed for this post. For more information, please take a look.

Reflected in this post:

  • Dependencies in Dart/Flutter
  • Code to authorize with the REST API
  • Code to get JSON data from the REST API

Not reflected in this post:

  • How to work with REST in the database (Please take a look on the posts from Jeff and here and here and here)
  • How to setup OAuth2 in the Oracle environment (Please take a look on these post and here)
  • How to create JSON data model in Flutter (Andrea offers a great post about that)
  • How to display the received JSON data in a Flutter widget

Before we start: On a high level you need Dart/Flutter installed and working on your client. Oracle database with the REST API available and Client_Id and Client_Secret created.

First we need 2 dependencies in our „pubspec.yaml„: „oauth2_client“ and „oauth2„.

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter
  intl: ^0.17.0
  http: ^0.13.4
  provider: ^6.0.1
  oauth2_client: ^2.3.1
  oauth2: ^2.0.0
  

Next we have to create our „MyOAuth2Client“ class extends from „OAuth2Client“ where we have to describe our „authorizeURL“ and our „tokenURL„. To connect and authorize with our REST API we use an „OAuth2Helper“ and add our „Client_Id“ and „Client_Secret„. In the code below named with „MyClientID“ and „MyClientSecret„. In „OAuth2Helper“ set „grantType:“ to „OAuth2Helper.CLIENT_CREDENTIALS„.

With this preparation we can call our „getPantryList“ within our Flutter widget to get a JSON list back from our Oracle database with all pantry entries. All the magic things (authorization, token handling) will happen without any further interaction from the user.

The variable „domain“ is predefined in the „secrets.dart“ and is imported on top of the code. I use this to not push private data into github.

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:oauth2_client/oauth2_helper.dart';
import 'package:oauth2_client/oauth2_client.dart';
import 'package:pantry/app/models/pantry.dart';
import 'package:pantry/app/auth/secrets.dart';
import 'package:pantry/app/models/pantryselect.dart';

class MyOAuth2Client extends OAuth2Client {
  MyOAuth2Client({required String redirectUri, required String customUriScheme})
      : super(
            authorizeUrl: domain + 'pantry/list',
            tokenUrl: domain + 'oauth/token',
            redirectUri: redirectUri,
            customUriScheme: customUriScheme);
}

OAuth2Client client = MyOAuth2Client(redirectUri: '', customUriScheme: '');

OAuth2Helper oAuth2Helper = OAuth2Helper(client,
    clientId: 'MyClientID',
    clientSecret: 'MyClientSecret',
    grantType: OAuth2Helper.CLIENT_CREDENTIALS);

Future<Pantrylist> getPantrylist() async {
  http.Response resp = await oAuth2Helper.get(domain + 'pantry/list');
  var pantrylistModel;
  try {
    if (resp.statusCode == 200) {
      var jsonString = resp.body;
      var jsonMap = json.decode(jsonString);

      pantrylistModel = Pantrylist.fromJson(jsonMap);
    }
  } on Exception {
    return pantrylistModel;
  }
  return pantrylistModel;
}

With a few more REST endpoints in my database I can add, change and delete entries if needed.

The beauty of using the Oracle free cloud for my data is, that I use the same data in an Oracle APEX application. With that I can create a GUI for the browser and work with the same data.

Dieser Beitrag wurde unter Dart, Flutter, Programmierung, REST API abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.