简体   繁体   English

从 Flutter 应用程序从 Microsoft AAD 检索 Object Id

[英]Retrieval of Object Id from Microsoft AAD from Flutter App

I'm trying to retrieve the Object id of the logged-in user from the flutter app.我正在尝试从 flutter 应用程序中检索登录用户的 Object id。

I have retrieved accessToken using https://pub.dev/packages/aad_oauth .我已经使用https://pub.dev/packages/aad_oauth 检索了 accessToken After that, I'm trying to retrieve from Microsoft graph API with a normal HTTP get method for which.之后,我尝试使用普通的 HTTP 获取方法从 Microsoft graph API 中检索。 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 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.亲爱的开发人员请帮助我解决此问题。

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并设置请求正文

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

{
      "accountEnabled": true,
      "displayName": "Adele Vance",
      "mailNickname": "AdeleV",
      "userPrincipalName": "AdeleV@contoso.onmicrosoft.com",
      "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.您正在使用带有https://graph.microsoft.com/v1.0/me端点的POST方法,没有任何请求正文。

See reference here .请参阅此处的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在flutter中集成Microsoft Graph API并从Microsoft Azure Active Directory中获取用户的对象ID? - How to integrate Microsoft Graph API and fetch the object ID of a user from Microsoft Azure Active Directory in flutter? 从 Flutter 应用程序获取父对象信息 - Getting parent object info from Flutter app Null 从 Flutter 中的 Firestore 检索数据后的列表 - Null List after data retrieval from Firestore in Flutter 未在 REST 服务器上验证来自 Flutter App 的 Firebase Id 令牌 - Firebase Id Token from Flutter App is not verified on the REST server 用 listview.builder 的 Flutter 中的 object 的 id 替换项目 - Replacing item by id of object in Flutter from listview.builder 我从 flutter 中的 package“aad_auth”收到“读取初始化”错误 - I am getting a "reading init" error from the package "aad_auth" in flutter 我如何在 Flutter 应用程序中保护我的 AAD 访问令牌 - How i Protect my AAD access Token in Flutter app Flutter:是否有必要在发布应用程序之前从 Firebase admob 中删除测试设备 ID? - Flutter: Is it neccessary to remove testdevice id from Firebase admob before publishing app? flutter:从 flutter 应用程序构建 apk 失败 - flutter: Build apk from flutter app is failed 如何在 Flutter 应用程序中的任何位置登录和访问后存储用户 ID 或“密钥”? - How to store a User id or "key" after login and access from anywhere in Flutter app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM