![](/img/trans.png)
[英]How to integrate Microsoft Graph API and fetch the object ID of a user from Microsoft Azure Active Directory in flutter?
[英]Retrieval of Object Id from Microsoft AAD from Flutter App
我正在嘗試從 flutter 應用程序中檢索登錄用戶的 Object id。
我已經使用https://pub.dev/packages/aad_oauth 檢索了 accessToken 。 之后,我嘗試使用普通的 HTTP 獲取方法從 Microsoft graph 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,
};
}
親愛的開發人員請幫助我解決此問題。
看起來用戶沒有成功創建。
要創建用戶,您應該調用此端點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"
}
}
您正在使用帶有https://graph.microsoft.com/v1.0/me
端點的POST
方法,沒有任何請求正文。
請參閱此處的參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.