问题描述
这是有关使用Graph API更新登录用户的个人资料信息的问题
我正在使用本教程,网址为(
我一直收到状态代码405错误,这意味着不允许使用HTTP方法.但是文档专门告诉我使用更新功能PATCH.
有人可以提供工作代码(和权限/作用域属性)来更新用户的属性,以便我知道正确的过程是什么.
谢谢!
更新9/22:
我还添加了全局管理员角色.不幸的是,我仍然收到405 Method ErrorMethodNotAllowed错误.
更新9/23:
在(
为用户提供Global Admin角色,然后尝试调用.看起来您正在直接使用MSA帐户.请获取Azure Active Directory用户的UserPrincipalName(UPN),然后使用这些凭据登录到Graph Explorer,并通过选择齿轮按钮来授予Directory.AccessAsUser.All权限->选择权限->Directory.AccessAsUser.All,然后尝试通过在JSON有效负载中提供officeLocation来进行更新.它对我有用.
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" }
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.
这篇关于使用Graph API更新用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!