Flutter app not being redirected to correct redirectUri from oauth2_client Flutter package

BACKGROUND

I’m relatively new to Flutter and trying to build a new app from scratch (currently just in development and only testing on Android right now) using the oauth2_client package to allow the user to connect the app with their Intuit Quickbooks account. The app correctly opens the authentication window and allows the user to log in to their Quickbooks.

ISSUE

After log in, Android navigates back to the app but it doesn’t navigate back to the correct screen based on the redirectUri passed to the OAuth2Client, it instead navigates back to the screen where the user initiated the authentication. I can’t figure out why this is happening.

CODE

app_routes.dart

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>final router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const AuthGate(),
routes: [
GoRoute(
path: 'about',
builder: (context, state) {
if(state.uri.queryParameters['realmId'] != null)
{
Provider.of<BooksmartOAuthHelper>(context, listen: false).setQuickbooksRealmId(state.uri.queryParameters['realmId']!);
}
return const AboutPage();
},
),
],
),
],
);
</code>
<code>final router = GoRouter( routes: [ GoRoute( path: '/', builder: (context, state) => const AuthGate(), routes: [ GoRoute( path: 'about', builder: (context, state) { if(state.uri.queryParameters['realmId'] != null) { Provider.of<BooksmartOAuthHelper>(context, listen: false).setQuickbooksRealmId(state.uri.queryParameters['realmId']!); } return const AboutPage(); }, ), ], ), ], ); </code>
final router = GoRouter(
  routes: [
    GoRoute(
      path: '/',
      builder: (context, state) => const AuthGate(),
      routes: [
        GoRoute(
          path: 'about',
          builder: (context, state) {
            if(state.uri.queryParameters['realmId'] != null)
            {
              Provider.of<BooksmartOAuthHelper>(context, listen: false).setQuickbooksRealmId(state.uri.queryParameters['realmId']!);
            }

            return const AboutPage();
          },
        ),
      ],
    ),
  ],
);

The authentication is initiated with this code from my HomeScreen:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>onTap: () {
Provider.of<BooksmartOAuthHelper>(context, listen: false).authenticateOAuth2Helper(app, context);
},
</code>
<code>onTap: () { Provider.of<BooksmartOAuthHelper>(context, listen: false).authenticateOAuth2Helper(app, context); }, </code>
onTap: () {
  Provider.of<BooksmartOAuthHelper>(context, listen: false).authenticateOAuth2Helper(app, context);
},

And here’s BooksmartOAuthHelper (I just made this wrapper so I could use it in a Provider at the top level of my app without having to instantiate the OAuth2Helper):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class BooksmartOAuthHelper {
OAuth2Helper? oauth2Helper;
String? quickbooksRealmId;
BooksmartOAuthHelper();
void authenticateOAuth2Helper(String accountingApp, BuildContext context) async {
switch (accountingApp) {
case 'Quickbooks':
IntuitOAuth2Client client = IntuitOAuth2Client(
redirectUri: 'https://booksmart-6870c.web.app/about',
customUriScheme: 'https'
);
oauth2Helper = OAuth2Helper(
client,
grantType: OAuth2Helper.authorizationCode,
clientId: intuitClientId,
clientSecret: intuitClientSecret,
scopes: ['com.intuit.quickbooks.accounting', 'com.intuit.quickbooks.payment'],
);
await oauth2Helper!.fetchToken();
default:
throw UnimplementedError('$accountingApp is not supported yet');
}
}
}
</code>
<code>class BooksmartOAuthHelper { OAuth2Helper? oauth2Helper; String? quickbooksRealmId; BooksmartOAuthHelper(); void authenticateOAuth2Helper(String accountingApp, BuildContext context) async { switch (accountingApp) { case 'Quickbooks': IntuitOAuth2Client client = IntuitOAuth2Client( redirectUri: 'https://booksmart-6870c.web.app/about', customUriScheme: 'https' ); oauth2Helper = OAuth2Helper( client, grantType: OAuth2Helper.authorizationCode, clientId: intuitClientId, clientSecret: intuitClientSecret, scopes: ['com.intuit.quickbooks.accounting', 'com.intuit.quickbooks.payment'], ); await oauth2Helper!.fetchToken(); default: throw UnimplementedError('$accountingApp is not supported yet'); } } } </code>
class BooksmartOAuthHelper {
  OAuth2Helper? oauth2Helper;
  String? quickbooksRealmId;

  BooksmartOAuthHelper();

  void authenticateOAuth2Helper(String accountingApp, BuildContext context) async {
    switch (accountingApp) {
      case 'Quickbooks':
        IntuitOAuth2Client client = IntuitOAuth2Client(
          redirectUri: 'https://booksmart-6870c.web.app/about',
          customUriScheme: 'https'
        );

        oauth2Helper = OAuth2Helper(
          client,
          grantType: OAuth2Helper.authorizationCode,
          clientId: intuitClientId,
          clientSecret: intuitClientSecret,
          scopes: ['com.intuit.quickbooks.accounting', 'com.intuit.quickbooks.payment'],
        );

        await oauth2Helper!.fetchToken();
    default:
      throw UnimplementedError('$accountingApp is not supported yet');
    }
  }
}

The authentication window correctly pops up when authenticateOAuth2Helper is called and allows the user to log in.

Here’s my custom OAuth2Client:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class IntuitOAuth2Client extends OAuth2Client {
IntuitOAuth2Client({required super.redirectUri, required super.customUriScheme}):
super(
authorizeUrl: 'https://appcenter.intuit.com/connect/oauth2',
tokenUrl: 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'
);
}
</code>
<code>class IntuitOAuth2Client extends OAuth2Client { IntuitOAuth2Client({required super.redirectUri, required super.customUriScheme}): super( authorizeUrl: 'https://appcenter.intuit.com/connect/oauth2', tokenUrl: 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer' ); } </code>
class IntuitOAuth2Client extends OAuth2Client {
  IntuitOAuth2Client({required super.redirectUri, required super.customUriScheme}): 
  super(
    authorizeUrl: 'https://appcenter.intuit.com/connect/oauth2',
    tokenUrl: 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'
  );
}

Here’s a code snippet from my AndroidManifest.xml:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><manifest ...>
<application ...>
<activity android:name=".MainActivity" ...>
</activity>
<meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
<activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true">
<intent-filter android:label="flutter_web_auth_2" android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="booksmart-6870c.web.app"
android:pathPrefix="/about" />
<data android:scheme="http" />
</intent-filter>
</activity>
</manifest>
</code>
<code><manifest ...> <application ...> <activity android:name=".MainActivity" ...> </activity> <meta-data android:name="flutter_deeplinking_enabled" android:value="true" /> <activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true"> <intent-filter android:label="flutter_web_auth_2" android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="booksmart-6870c.web.app" android:pathPrefix="/about" /> <data android:scheme="http" /> </intent-filter> </activity> </manifest> </code>
<manifest ...>
    <application ...>
        <activity android:name=".MainActivity" ...>
        </activity>
        <meta-data android:name="flutter_deeplinking_enabled" android:value="true" />
        <activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true">
            <intent-filter android:label="flutter_web_auth_2" android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" 
                    android:host="booksmart-6870c.web.app" 
                    android:pathPrefix="/about" />
                <data android:scheme="http" />
            </intent-filter>
        </activity>
</manifest>

Also, since Quickbooks requires an http or https scheme, I’ve set up my callback URI as an App Link. I’m hosting a dummy website on Firebase Hosting (https://booksmart-6870c.web.app) and the web server contains the /.well-known/assetlinks.json so Android knows to redirect these URL calls to my app.

Other interesting notes:

  • Before I added the <activity android:name="com.linusu.flutter_web_auth_2.CallbackActivity" android:exported="true"> ... </activity> to the AndroidManifest.xml and instead had the same intent-filter in the MainActivity, the authentication wasn’t working but after the authentication failed I would still get redirected to my AboutPage with the correct query parameters
  • By setting breakpoints in my app after the oauth2Helper!.fetchToken() call I’m able to confirm that the authentication is working properly because the oauth2Helper has an accessToken

PROBLEM QUESTION
Does anyone know why my app isn’t correctly redirecting to the AboutPage (the end of the redirect URI based on my GoRouter)? It’s instead redirecting back to the HomePage on the app (where the authentication was initiated). I need to get the query parameters from the redirect URI, but because the redirect URI isn’t being handled by my app correctly I’m unable to get those parameters.

SUMMARY

What I tried:

  • Tapped the button that called authenticateOAuth2Helper
  • logged in to my Quickbooks account successfully

What I expected to happen:

  • get redirected back to my App’s AboutPage

What actually resulted:

  • got redirected back to my App’s HomePage

New contributor

Jay Boychuk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật