fferrin
fferrin

Reputation: 908

OAuth2 authentication in Flutter

I can not authenticate with Flutter and OAuth2. Basically I created a Flutter base project and added the example of the OAuth2 wiki.

This is the class from Flutter basic project:

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _login() async {
    final authorizationEndpoint =
    Uri.parse("http://blablablabla/o/token");

    final username = "username";
    final password = "password";

    // Something like this
    final identifier = "mavEZjJgs9d4JwvvXsANZgN5Dz5GFxzfj616752A";
    final secret = "hcAFRwvtGqzhKrMPH2Vqm1vncuZt2YTVfTs6LcdNcnKPdEH3J0T1njIwurryofrvDMnzOvhQDVbaC9Gt5DctciTv3n89s7JSGjpHtzkbEfLpkOT5y6YHN3p6grQlYGd59";

    // Make a request to the authorization endpoint that will produce the fully
    // authenticated Client.
    var client = await oauth2.resourceOwnerPasswordGrant(
        authorizationEndpoint, username, password,
        identifier: identifier, secret: secret);

    // Once you have the client, you can use it just like any other HTTP client.
    var result = await client.read("http://blablabla/api/users/me/");

    // Once we're done with the client, save the credentials file. This will allow
    // us to re-use the credentials and avoid storing the username and password
    // directly.
    new File("~/.myapp/credentials.json")
        .writeAsString(client.credentials.toJson());
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
//        onPressed: _incrementCounter,
        onPressed: _login,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

I changed the _incrementCounter method of the tutorial to _login. When I press the button (which I should now log me in) I get the following:

E/flutter ( 7662): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7662): NoSuchMethodError: No top-level method 'base64Encode' declared.
E/flutter ( 7662): Receiver: top-level
E/flutter ( 7662): Tried calling: base64Encode(Uint8Array)
E/flutter ( 7662): #0      NoSuchMethodError._throwNew (dart:core-patch/dart:core/errors_patch.dart:192)
E/flutter ( 7662): #1      basicAuthHeader (package:oauth2/src/utils.dart:14)
E/flutter ( 7662): #2      resourceOwnerPasswordGrant (package:oauth2/src/resource_owner_password_grant.dart:69)
E/flutter ( 7662): <asynchronous suspension>
E/flutter ( 7662): #3      _MyHomePageState._login (file:///home/thisismyuser/AndroidStudioProjects/login/lib/main.dart:79)
E/flutter ( 7662): <asynchronous suspension>
E/flutter ( 7662): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:478)
E/flutter ( 7662): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:530)
E/flutter ( 7662): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102)
E/flutter ( 7662): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161)
E/flutter ( 7662): #8      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123)
E/flutter ( 7662): #9      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156)
E/flutter ( 7662): #10     BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147)
E/flutter ( 7662): #11     BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121)
E/flutter ( 7662): #12     BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101)
E/flutter ( 7662): #13     BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64)
E/flutter ( 7662): #14     BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48)
E/flutter ( 7662): #15     _invoke1 (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:134)
E/flutter ( 7662): #16     _dispatchPointerDataPacket (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:91)

Thanks in advance!

(With Postman I get this beautiful authentication)

enter image description here

Upvotes: 2

Views: 13669

Answers (2)

aubykhan
aubykhan

Reputation: 264

It's fairly straight-forward to use standard http client for oauth requests. Have a look at a simple library I've created on GitHub. Particularly, have a look at Authenticator class.

Upvotes: -1

wasyl
wasyl

Reputation: 3506

My guess is that you're using Dart 1, while the oauth library depends on Dart 2 APIs (seems like base64Encode method was added only in Dart 2). Make sure you're using at least Flutter Beta 2, which uses Dart 2 by default. If you're using Dart 1 intentionally, it seems like you'll have to upgrade.

You can make sure you're running the most recent Flutter beta build by calling in your terminal

flutter channel beta
flutter upgrade

With such questions it's also useful to provide the output of flutter doctor

Upvotes: 2

Related Questions