I am using Microsoft Graph API in my SPFx website. I can successfully get accessToken
using ADAL.js. I can create user, get users, get groups but when i am trying to add members in group it throws error of Unauthorized.
I had assiged full permission to app resource and my signed in user is global administrator.
With graph explorer same request is working fine.
All parameters value passes are true that I have verified.
private AddUserInGroup(httpClient: HttpClient, accessToken: string, objUser: IUser, groupId: string): Promise<boolean> {
return new Promise<boolean>((resolve: (isAdded: boolean) => string, reject: (error: string) => string): void => {
httpClient.post("/groups/" + groupId + "/members/$ref", HttpClient.configurations.v1, {
headers: {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + accessToken
},
body: "{ \"@odata.id\": \"https://graph.microsoft.com/v1.0/users/" + objUser.id + "\"}"
})
.then((response: HttpClientResponse): void => {
debugger;
if (response.ok)
resolve(true);
else
reject(response.statusText);
}), (error: any): void => {
debugger;
alert(error.message);
if (error.message.toLowerCase() == "failed to fetch")
resolve(true);
else {
alert("Error : " + error.message);
reject(error.message);
}
};
});
}
If anyone knows answer then please let me know. Thanks in advance.