Update User info using Graph API

丶灬走出姿态 提交于 2021-02-11 13:40:41

问题


This is a question about updating a signed-in User's profile info using Graph API

I am using the tutorial at (https://docs.microsoft.com/en-us/graph/tutorials/javascript). It lets you:

  • sign in
  • get signed in user info
  • get calendar events
  • create calendar events

It works perfectly and looks great.

However, I'm having a hard time updating the user info. I stripped the sample code down to just getting the user info because I'm not dealing with calendar events.

Getting an access token, creating a client, and getting user profile works fine:

// Create an authentication provider
const authProvider = {
   
    getAccessToken: async () => {
      console.log('authProvider');
      return await getToken();
    }
  };
  
  // Initialize the Graph client
  const graphClient = MicrosoftGraph.Client.initWithMiddleware({authProvider});

  async function getUser() {

    return await graphClient
      .api('/me')
      // Only get the fields used by the app
      .select('id,displayName,userPrincipalName')
      .get();
  }

I replaced the content of the function getEvents() to update the user.

let user = {
        officeLocation: "Home Sweet Home"
    }

    try {
      return await graphClient.api('/me').update(user);
    }
    catch (error) {
      console.log(error);
      updatePage(Views.error, {
        message: 'Error logging in',
        debug: error
      });
    }

(https://docs.microsoft.com/en-us/graph/api/user-update)

I wanted to update something basic like a User's officeLocation, however it did not work.

I correctly gave the proper permissions and scope in my App's permissions like so:

I keep getting Status Code 405 error, which means the HTTP method is not allowed. But documentation specifically told me to use the update function, which is PATCH.

Can someone provide working code (and permissions/scope attributes) to update a User's property so I know what the proper process is.

Thank you!

Update 9/22:

I also added the Global Administrator Role. Unfortunately, I'm still getting 405 Method ErrorMethodNotAllowed error.

Update 9/23:

Used the Graph Explorer at (https://developer.microsoft.com/en-us/graph/graph-explorer). When I attempted to use 'patch' on '/me', there is no permissions for me to consent to. And therefore, queries fail with "ErrorMethodNotAllowed".

The body is: { "jobTitle": "test" }


回答1:


Give the user a Global Admin role and then try to call. Looks like you are directly using the MSA account. Please get the UserPrincipalName(UPN) of the Azure Active Directory user and then login to the Graph Explorer with those credientials and give Directory.AccessAsUser.All permission by selecting the gear button--> select Permission --> Directory.AccessAsUser.All and then try to update by giving officeLocation in JSON payload. It worked for me.



来源:https://stackoverflow.com/questions/63988409/update-user-info-using-graph-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!