[英]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
方法,没有任何请求正文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.