2

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

4
  • Hi @nikolat328, Any luck? I am also stuck with this ... Commented Jun 26, 2018 at 8:36
  • Hi! Nothing yet...I'll probably ask on Twitter Commented Jun 28, 2018 at 12:55
  • @nikolat328 Can you please paste output of flutter doctor? Commented Aug 5, 2018 at 15:09
  • @wasyl this is the output: pastebin.com/YCRx8qvY Commented Jul 12, 2019 at 0:45

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm using Dart 2.4.0. I'm having another error now (maybe because I was using Dart 1 before). Here is the console log: pastebin.com/8zjsKuzX, and the outputflutter doctor: pastebin.com/YCRx8qvY
@nikolat328 the error you have now is completely different from the original, so I suggest that you ask a new question. It seems like you're providing localhost:45306 as the autorization endpoint, but the server there doesn't exist or doesn't accept connections
-1

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.

2 Comments

The library uses client secret which shouldn't be made public on a mobile app. One should instead be using a code grant flow.
Good point. However, that's just an example of how things could work out which can be easily customized to remove the keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.