1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter: Unable to create MSALPublicClientApplication

Discussão em 'Mobile' iniciado por Stack, Novembro 8, 2024 às 11:02.

  1. Stack

    Stack Membro Participativo

    I am using the msal_auth library to log in.

    First of all, I am trying the example app to test on ios.

    import 'dart:developer';

    import 'package:flutter/material.dart';
    import 'package:flutter_dotenv/flutter_dotenv.dart';
    import 'package:msal_auth/msal_auth.dart';

    Future<void> main() async {
    await dotenv.load(fileName: "assets/.env");
    runApp(const MyApp());
    }

    class MyApp extends StatefulWidget {
    const MyApp({super.key});

    @override
    State<MyApp> createState() => _MyAppState();
    }

    class _MyAppState extends State<MyApp> {
    final _clientId = dotenv.env['MS_CLIENT_ID'];
    final _tenantId = dotenv.env['MS_TENANT_ID'];
    late final _authority =
    'https://login.microsoftonline.com/$_tenantId/oauth2/v2.0/authorize';
    final _scopes = <String>[
    'https://graph.microsoft.com/user.read',
    ];

    @override
    Widget build(BuildContext context) {
    return MaterialApp(
    home: Scaffold(
    appBar: AppBar(
    title: const Text('MSAL example app'),
    ),
    body: Center(
    child: Column(
    mainAxisSize: MainAxisSize.min,
    children: [
    ElevatedButton(
    onPressed: getToken,
    child: const Text('Get Token'),
    ),
    const SizedBox(height: 24),
    ElevatedButton(
    onPressed: getTokenSilently,
    child: const Text('Get Token silent'),
    ),
    const SizedBox(height: 24),
    ElevatedButton(
    onPressed: logout,
    child: const Text('Logout'),
    ),
    ],
    ),
    ),
    ),
    );
    }

    Future<MsalAuth> getMsalAuth() async {
    return MsalAuth.createPublicClientApplication(
    clientId: _clientId!,
    scopes: _scopes,
    androidConfig: AndroidConfig(
    configFilePath: 'assets/msal_config.json',
    tenantId: _tenantId,
    ),
    iosConfig: IosConfig(authority: _authority),
    );
    }

    Future<void> getToken() async {
    try {
    final msalAuth = await getMsalAuth();
    final user = await msalAuth.acquireToken();
    log('User data: ${user?.toJson()}');
    } on MsalException catch (e) {
    log('Msal exception with error: ${e.errorMessage}');
    } catch (e) {
    log(e.toString());
    }
    }

    Future<void> getTokenSilently() async {
    try {
    final msalAuth = await getMsalAuth();
    final user = await msalAuth.acquireTokenSilent();
    log('User data: ${user?.toJson()}');
    } on MsalException catch (e) {
    log('Msal exception with error: ${e.errorMessage}');
    } catch (e) {
    log(e.toString());
    }
    }

    Future<void> logout() async {
    try {
    final msalAuth = await getMsalAuth();
    await msalAuth.logout();
    } on MsalException catch (e) {
    log('Msal exception with error: ${e.errorMessage}');
    } catch (e) {
    log(e.toString());
    }
    }
    }


    I am getting this error on all 3 buttons
    [log] Msal exception with error: Unable to create MSALPublicClientApplication

    This is my info.plist

    <key>CFBundleURLTypes</key>
    <array>
    <dict>
    <key>CFBundleURLName</key>
    <string>{app_bundle_id}</string>
    <key>CFBundleURLSchemes</key>
    <array>
    <string>msauth.{app_bundle_id}</string>
    </array>
    </dict>
    </array>

    <key>LSApplicationQueriesSchemes</key>
    <array>
    <string>msauthv2</string>
    <string>msauthv3</string>
    </array>


    I have tried hardcoded Client Id and Tenant Id too but I am getting same error. I am using the Flutter 3.24.4 and msal:2.1.0

    I have also added keychain sharing in XCode signin and capabilities.

    Continue reading...

Compartilhe esta Página