[英]Inviting a User in Azure AD through Microsoft Graph API doesn't work
以下是我在Azure AD中邀請用戶使用的代碼。
我收到“未經授權”的回復。 我不確定缺少哪些權限/設置。 有誰有主意。
string accessToken = await AuthenticationHelper.GetTokenForApplication ();
InvitationModel invite = new InvitationModel ();
invite.invitedUserEmailAddress = user.Email;
invite.inviteRedirectUrl = ConfigurationManager.AppSettings["InviteRedirectUrl"];
invite.sendInvitationMessage = true;
using (HttpClient client = new HttpClient ()) {
client.BaseAddress = new Uri ("https://graph.microsoft.com");
client.DefaultRequestHeaders.Accept.Add (
new MediaTypeWithQualityHeaderValue ("application/json"));
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue ("Bearer", accessToken);
HttpResponseMessage response =
client.PostAsJsonAsync<InvitationModel> ("v1.6/invitations", invite).Result;
dynamic inviteResult =
response.Content.ReadAsAsync<dynamic> ().Result;
if (inviteResult.status != "Error") { }
}
您的問題在於,您在此處將Microsoft Graph和Azure AD Graph進行了合並。 這是兩個不同的API,具有不同的調用轉換和權限范圍。
為了創建邀請,您將需要以下許可權范圍之一 ( 請注意,第一個是(全局性)限制性最強的許可權,最后一個是許可性最強的許可權 ):
User.Invite.All
User.ReadWrite.All
Directory.ReadWrite.All
請注意,所有這些范圍都是受管理員限制的,在使用它們之前需要獲得管理員同意
獲得有效令牌后,您需要使用以下主體對: https://graph.microsoft.com/v1.0/invitations
進行POST
調用:
{
"invitedUserEmailAddress": "yyy@test.com",
"inviteRedirectUrl": "https://myapp.com"
}
由於您使用的是C#,因此我強烈建議您使用Microsoft Graph Client Library,而不是手動滾動自己的HttpClient
調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.