Unauthorized Microsoft Graph API Add member in group.

%3CLINGO-SUB%20id%3D%22lingo-sub-82731%22%20slang%3D%22en-US%22%3EUnauthorized%20Microsoft%20Graph%20API%20Add%20member%20in%20group.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-82731%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20using%20Microsoft%20Graph%20API%20in%20my%20SPFx%20website.%20I%20can%20successfully%20get%20accessToken%20using%20ADAL.js.%20I%20can%20create%20user%2C%20get%20users%2C%20get%20groups%20but%20when%20i%20am%20trying%20to%20add%20memebrs%20in%20group%20it%20throws%20error%20of%20Unauthorized.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20had%20assiged%20full%20permission%20to%20app%20resource%20and%20my%20signed%20in%20user%20is%20global%20administrator.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20graph%20explorer%20same%20request%20is%20working%20fine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20parameters%20value%20passes%20are%20true%20that%20i%20have%20verified.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%3EAddUserInGroup%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3EhttpClient%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3EHttpClient%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EaccessToken%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3Estring%3C%2FSPAN%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CSPAN%3EobjUser%3C%2FSPAN%3E%3CSPAN%3E%3A%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3CSPAN%3E%7D%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ebody%3A%3C%2FSPAN%3E%20%3CSPAN%3E%22%7B%20%5C%22%40odata.id%5C%22%3A%20%5C%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fusers%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fusers%2F%3C%2FA%3E%22%3C%2FSPAN%3E%20%3CSPAN%3E%2B%3C%2FSPAN%3E%20%3CSPAN%3EobjUser%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Eid%3C%2FSPAN%3E%20%3CSPAN%3E%2B%3C%2FSPAN%3E%20%3CSPAN%3E%22%5C%22%7D%22%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%7D)%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%3Ethen%3C%2FSPAN%3E%3CSPAN%3E((%3C%2FSPAN%3E%3CSPAN%3Eresponse%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3EHttpClientResponse%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%20%3CSPAN%3Evoid%3C%2FSPAN%3E%20%3CSPAN%3E%3D%26gt%3B%3C%2FSPAN%3E%3CSPAN%3E%20%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Edebugger%3C%2FSPAN%3E%3CSPAN%3E%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Eif%3C%2FSPAN%3E%3CSPAN%3E%20(%3C%2FSPAN%3E%3CSPAN%3Eresponse%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%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V%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EIf%20anyone%20knows%20answer%20then%20please%20let%20me%20know.%20Thanks%20in%20advance.%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-82731%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-82741%22%20slang%3D%22en-US%22%3ERe%3A%20Unauthorized%20Microsoft%20Graph%20API%20Add%20member%20in%20group.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-82741%22%20slang%3D%22en-US%22%3E%3CP%3EI%20read%20it%20and%20it%20seems%20that%20it%20will%20not%20work%20with%20Internet%20Explorer%20and%20mixed%20zones.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20able%20to%20get%20accessToken%20and%20can%20run%20other%20apis%20successfully%20except%20Add%20members%20in%20group%20so%20do%20you%20think%20that%20it%20is%20related%20to%20adal.js%20(mixed%20zones).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20confirm%20that%20whether%20it%20will%20work%20or%20not%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20not%20then%20can%20you%20provide%20alternative%20solution%20for%20it.%20As%20i%20want%20to%20develop%20one%20functionality%20which%20requires%20this%20features.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20your%20prompt%20reply.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-82736%22%20slang%3D%22en-US%22%3ERe%3A%20Unauthorized%20Microsoft%20Graph%20API%20Add%20member%20in%20group.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-82736%22%20slang%3D%22en-US%22%3EHave%20you%20seen%20this%20discussion%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2Fsp-dev-fx-webparts%2Fissues%2F36%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2Fsp-dev-fx-webparts%2Fissues%2F36%3C%2FA%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello,

 

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 memebrs 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.
2 Replies

I read it and it seems that it will not work with Internet Explorer and mixed zones.

 

I can able to get accessToken and can run other apis successfully except Add members in group so do you think that it is related to adal.js (mixed zones).

 

Can you confirm that whether it will work or not?

 

If not then can you provide alternative solution for it. As i want to develop one functionality which requires this features.

 

Thanks for your prompt reply.