How do I fix this error with the Twitter authentification with Flutter and Firebase?

1k Views Asked by At

I keep getting this error while trying to set the Twitter authentification on my Flutter application.

Here's the error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(400, Failed to generate request token., Please check your APIKey or APISecret., null)

Here's my function :

    Future<UserCredential> signInWithTwitter() async {
    final twitterLogin = TwitterLogin(
    apiKey: '...',
    apiSecretKey:'...',
    redirectURI: 'http://localhost:8080/auth/twitter/callback',
    );
    final authResult = await twitterLogin.login();
    final twitterAuthCredential = TwitterAuthProvider.credential(
      accessToken: authResult.authToken!,
     secret: authResult.authTokenSecret!,
  );
    return await FirebaseAuth.instance.signInWithCredential(twitterAuthCredential);
  }

And here's how I call it with a button :

onPressed: () async {
     await signInWithTwitter();
     Navigator.push(
                 context,
                 MaterialPageRoute(
                 builder: (BuildContext context) => const NextPage()));
},

Here's my pubspec.yaml:

firebase_core: ^1.12.0
firebase_auth: ^3.3.7
google_sign_in: ^5.2.4
twitter_login: ^4.0.1

Thanks for your help!

2

There are 2 best solutions below

0
Visal SUOS On

Please use check my code below

final twitterLogin = TwitterLogin(
  /// Consumer API keys
  apiKey: 'mX....S5',

  /// Consumer API Secret keys
  apiSecretKey: 'YU....JRjK',

  /// Registered Callback URLs in TwitterApp
  /// Android is a deeplink
  /// iOS is a URLScheme
  redirectURI: '.....',
);

If you cannot find it, please regenerate your consumer key enter image description here

0
OmarYehiaDev On

Okay, after some search and work I found the solution was to add twittersdk:// to your Callback URI in Twitter Dashboard Here is a reference link to the solution from Github