0

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.

1
  • Where did you take the code from? If it's not your original piece of art, you need to add a reference to it.
    – moe
    Commented Jun 28, 2017 at 7:36

1 Answer 1

0
httpClient.post("/groups/" + groupId + "/members/$ref", HttpClient.configurations.v1, {

Post Url goes wrong. It takes SharePoint site url. So we have to write full url to call instead of starting /groups

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.