0

我正在尝试从颤振应用程序中检索登录用户的对象 ID。

我已经使用https://pub.dev/packages/aad_oauth检索了 accessToken 。之后,我尝试使用普通的 HTTP 获取方法从 Microsoft 图形 API 中检索。我收到此错误:

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

Receiver: null

Tried calling: id

以下是代码片段:

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,
  };
}

亲爱的开发人员请帮助我解决此问题。

4

1 回答 1

0

看起来用户没有成功创建。

要创建用户,您应该调用此端点 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"
      }
}

您正在使用没有任何请求正文POST的端点方法。https://graph.microsoft.com/v1.0/me

请参阅此处的参考。

于 2020-12-10T02:32:03.140 回答