flutter: HttpException: Failed Forbidden from the twitter plugin

239

Solution 1

So, after a little bit of digging I found the answer to my question. In order to make it work I did the following:

  1. changed the android scheme to appname://

  2. removed the android host

    <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE"/>
     <!-- Accepts URIs that begin with "example://gizmos” -->
     <!-- Registered Callback URLs in TwitterApp -->
     <data android:scheme="appname" />
         <!-- host is option -->
     </intent-filter>
    
  3. changed the redirect url inside the twitter config to appname://

  4. got elevated access for the twitter portal

  5. used the loginV2 function along with OAuth2 instead of OAuth1

     Future<UserCredential> _signInWithTwitter() async {
       // Create a TwitterLogin instance
    
    
      final twitterLogin = TwitterLogin(
           apiKey: '123',
           apiSecretKey: '1234',
           redirectURI: 'appname://');
    
       // Trigger the sign-in flow
       final authResult = await twitterLogin.loginV2();
       print(authResult.toMap());
    
       // Create a credential from the access token
       final twitterAuthCredential = TwitterAuthProvider.credential(
         accessToken: authResult.authToken!,
         secret: authResult.authTokenSecret!,
       );
    
       // Once signed in, return the UserCredential
       return await FirebaseAuth.instance
           .signInWithCredential(twitterAuthCredential);
     }
    
  6. Didn't use the callback provided by firebase at all (this is mentioned in the README too, but I'm too stupid to check)

Solution 2

Your getting Forbidden http exception. So, in the official documentation it says -

The request is understood, but it has been refused or access is not allowed. An accompanying error message will explain why.

And the solution is given as -

Check that your developer account includes access to the endpoint you’re trying to use. You may also need to get your App allowlisted (e.g. Engagement API or Ads API) or sign up for access.

You can check - Twitter API Documentation

Hope it helps.

Share:
239
Robert Pietraru
Author by

Robert Pietraru

Updated on December 01, 2022

Comments

  • Robert Pietraru
    Robert Pietraru over 1 year

    I'm trying to implement twitter login in my app but it doesn't work, returning an errorMessage in the AppResult object. Does anyone know a solution?

    The packages I use are twitter_login: ^4.2.3

    firebase: firebase_core: ^1.11.0 firebase_auth: ^3.3.5

    Twitter config (User authentication settings page):

    Firebase config:

    • twitter auth enabled
    • api key set (checked it like 10 times)
    • api secret set (same thing)

    Android manifest:

    inside the activity tag:

     <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!-- Accepts URIs that begin with "example://gizmos” -->
                <!-- Registered Callback URLs in TwitterApp -->
                <data android:scheme="https" android:host="app-name.firebaseapp.com" />
                <!-- host is option -->
            </intent-filter>
    

    after the activity tag:

    <meta-data android:name="flutterEmbedding" android:value="2" />
    

    The code itself:

     final twitterLogin = TwitterLogin(
          apiKey: '123 it's the same one',
          apiSecretKey: 'proper one',
          redirectURI: 'https://app-name.firebaseapp.com/__/auth/handler');
    
      final authResult = await twitterLogin.login();
      print(authResult.errorMessage); // prints out HttpException: Failed Forbidden
    

    The code opens the link with the authentication, but after clicking on "authorize app", it returns to the app with the errorMessage "HttpException: Failed Forbidden" Also, the authToken and the authTokenSecret are both null.

    If you need any additional information, please let me know!