Retrieval of Object Id from Microsoft AAD from Flutter App

852 views Asked by At

I'm trying to retrieve the Object id of the logged-in user from the flutter app.

I have retrieved accessToken using https://pub.dev/packages/aad_oauth. After that, I'm trying to retrieve from Microsoft graph API with a normal HTTP get method for which. I'm getting this error:

NoSuchMethodError: The getter 'id' was called on null.

Receiver: null

Tried calling: id

Following is the code snippet:

Future<InfoMe> createUser(String accessToken) async{
final String apiUrl = 'https://graph.microsoft.com/v1.0/me';
final response =
await http.get(apiUrl,headers: { "Authorization": "Bearer $accessToken", "Content-Type": "application/json"});
if (response.statusCode == 201) {
final String responseString = response.body;
    print('$responseString');
return infoMeFromJson(responseString);
  } else {
return null;
  }
}
void login() async {
try {
await oauth.login();
oauth.getIdToken().toString();
var accessToken = await oauth.getAccessToken(); //$accessToken
    final InfoMe info = await createUser(accessToken);
information = InfoMe(id: info.id);
    setState(() {
information = info;
    });
    showMessage("${information.displayName}" + " "+ '${information.id}');
 } catch (e) {
    showError(e);
  }
}

InfoMe.dart

import 'dart:convert';
InfoMe infoMeFromJson(String str) => InfoMe.fromJson(json.decode(str));
String infoMeToJson(InfoMe data) => json.encode(data.toJson());
class InfoMe {
  InfoMe({
    this.odataContext,
    this.businessPhones,
    this.displayName,
    this.givenName,
    this.jobTitle,
    this.mail,
    this.mobilePhone,
    this.officeLocation,
    this.preferredLanguage,
    this.surname,
    this.userPrincipalName,
    this.id,
  });
  String odataContext;
  List<String> businessPhones;
  String displayName;
  String givenName;
  String jobTitle;
  String mail;
  dynamic mobilePhone;
  dynamic officeLocation;
  dynamic preferredLanguage;
  dynamic surname;
  String userPrincipalName;
  String id;
  factory InfoMe.fromJson(Map<String, dynamic> json) => InfoMe(
    odataContext: json["@odata.context"],
    businessPhones: List<String>.from(json["businessPhones"].map((x) => x)),
    displayName: json["displayName"],
    givenName: json["givenName"],
    jobTitle: json["jobTitle"],
    mail: json["mail"],
    mobilePhone: json["mobilePhone"],
    officeLocation: json["officeLocation"],
    preferredLanguage: json["preferredLanguage"],
    surname: json["surname"],
    userPrincipalName: json["userPrincipalName"],
    id: json["id"],
  );
  Map<String, dynamic> toJson() => {
    "@odata.context": odataContext,
    "businessPhones": List<dynamic>.from(businessPhones.map((x) => x)),
    "displayName": displayName,
    "givenName": givenName,
    "jobTitle": jobTitle,
    "mail": mail,
    "mobilePhone": mobilePhone,
    "officeLocation": officeLocation,
    "preferredLanguage": preferredLanguage,
    "surname": surname,
    "userPrincipalName": userPrincipalName,
    "id": id,
  };
}

Dear developers kindly help me out to resolve this issue.

1

There are 1 answers

5
Allen Wu On

Looks like the user is not created successfully.

For creating a user, you should call this endpoint POST https://graph.microsoft.com/v1.0/users and set the request body:

POST https://graph.microsoft.com/v1.0/users

{
      "accountEnabled": true,
      "displayName": "Adele Vance",
      "mailNickname": "AdeleV",
      "userPrincipalName": "[email protected]",
      "passwordProfile" : {
        "forceChangePasswordNextSignIn": true,
        "password": "xWwvJ]6NMw+bWH-d"
      }
}

You are using POST method with https://graph.microsoft.com/v1.0/me endpoint without any request body.

See reference here.