error:code:Authorization_RequestDenied","message":"Insufficient privileges to complete the operation.

Ganesh Bhadane 1 Reputation point
2021-10-04T13:07:06.293+00:00

I'm tring to get data of signed in user from microsoft azure using microsoft graph api, i'm getting access token and also login successfully,

but i'm getting error as : "Insufficient privileges to complete the operation."

My Code:

import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:aad_oauth/aad_oauth.dart';
import 'package:aad_oauth/model/config.dart';
import 'package:exim_bank_leave_app/Utils/AppTheme.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'Screens/Listing.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: MyTheme.darkTheme(context),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  static var user;
  static final Config config = Config(
    tenant: 'f6f3ce---------298738',
    clientId: 'cba06e50-faa9--------2f5',
    scope: 'openid profile offline_access',
    redirectUri: 'https://login.microsoftonline.com/common/oauth2/nativeclient',
  );

  static final AadOAuth oauth = AadOAuth(config);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  SharedPreferences prefs;
  static final String accessToken = accessToken;
  var token;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    // adjust window size for browser login
    var screenSize = MediaQuery.of(context).size;
    var rectSize =
        Rect.fromLTWH(0.0, 25.0, screenSize.width, screenSize.height - 25);
    MyHomePage.oauth.setWebViewScreenSize(rectSize);

    return Scaffold(
      body: Container(),
    );
  }

  loadData() async {
    var auth = MyHomePage.oauth;
    await auth.login();
    var accessToken = await auth.getAccessToken();

    Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (context) => Listing()));

    prefs = await SharedPreferences.getInstance();
    while (prefs == null || accessToken == null) continue;
    final String idToken = await auth.getIdToken();

    setState(() {
      prefs.setString('token', idToken);
    });

    print('Token $accessToken');
    final graphResponse = await http.get('https://graph.microsoft.com/v1.0/me',
        headers: {HttpHeaders.authorizationHeader: "Bearer $accessToken"});
    print(graphResponse.body.toString());
    print(graphResponse.statusCode);
  }

  static void logout() async {
    await MyHomePage.oauth.logout();
  }
}
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,808 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Danstan Onyango 3,821 Reputation points Microsoft Employee
    2021-10-06T11:58:42.013+00:00

    Check the answer provided on this thread and let us know if it solves your scenario. This issue happens when the permissions are not granted by admin.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.