Earlybyte / aad_oauth

Flutter Azure Active Directory OAuth Package
https://pub.dev/packages/aad_oauth
MIT License
92 stars 179 forks source link

Successful Authentication, but Failed Retrieval of Microsoft User Information (403 Error) #312

Closed tratum closed 3 months ago

tratum commented 4 months ago

When using the aad_oauth package to authenticate a Microsoft user and obtaining the accessToken successfully, attempts to retrieve user information result in a 403 error.

Source Codes used:

aad_oauth configuration

  static const String tenant = "organizations";
  static const String redirectUrl = "msauth://com.******.***********/2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D";
  static const String scope = "openid profile email offline_access";
  static const String responseType = "code";

  static final Config _config = Config(
    tenant: tenant,
    clientId: clientID,
    scope: scope,
    navigatorKey: navigatorKey,
    loader: const SizedBox(),
    responseType: responseType,
    // appBar: AppBar(
    //   title: const Text(''),
    // ),
  );

  static final AadOAuth oauth = AadOAuth(_config);

Getting User Profile

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

const storage = FlutterSecureStorage();
final dio = Dio();

Future<void> getUserProfile() async {
  final accessToken = await storage.read(key: 'access_token');
  if (accessToken != null) {
    final response = await dio.get(
      'https://graph.microsoft.com/v1.0/me/messages',
      options: Options(headers: {
        'Authorization': 'Bearer $accessToken',
      }),
    );

    if (response.statusCode == 200) {
      // Handle the response
      debugPrint('-------------Response body: ${response.data}');
    } else {
      // Handle the error
      debugPrint('Request failed with status: ${response.statusCode}.');
    }
  } else {
    debugPrint('Access token not found.');
  }
}

Final Operation

                  final result = await MicrosoftAuth.oauth.login();
                  result.fold(
                    (l) => debugPrint('--------------------Error: $l'),
                    (r) {
                      String? accessToken = r.accessToken;
                      String? idToken = r.idToken;
                      String? refreshToken = r.refreshToken;
                      String? tokenType = r.tokenType;
                      int? expiresIn = r.expiresIn;
                      saveAccessToken(accessToken);
                      saveIDToken(idToken);
                      getUserProfile();
                      Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                            builder: (context) => const Dashboard()
                        ),
                      );
                    },
                  );

*Azure Portal

Screenshot from 2024-03-11 14-29-58 Screenshot from 2024-03-11 14-30-21

tratum commented 4 months ago

This is not a package issue but a personal problem i think so if anyone can help much appreciated @loetsphi or @josemiguelvarela

tratum commented 3 months ago

Can anyone help

tratum commented 3 months ago

If Anyone else is also having the Same Problem then NO Need to worry it's resolved

Change in the Getting User Profile Flow

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

const storage = FlutterSecureStorage();
final dio = Dio();

Future<void> getUserProfile() async {
  final accessToken = await storage.read(key: 'access_token');
  if (accessToken != null) {
    final response = await dio.get(
      'https://graph.microsoft.com/v1.0/me',  //---------------------------------Change Made in The Line
      options: Options(headers: {
        'Authorization': 'Bearer $accessToken',
      }),
    );

    if (response.statusCode == 200) {
      // Handle the response
      debugPrint('-------------Response body: ${response.data}');
    } else {
      // Handle the error
      debugPrint('Request failed with status: ${response.statusCode}.');
    }
  } else {
    debugPrint('Access token not found.');
  }
}