简体   繁体   English

后台访问microsoft Graph API

[英]Background Access to microsoft Graph API

i'm developing a single page application for a costumer.我正在为客户开发单页应用程序。 I need to create a script that allows me to access to my app without user interaction.我需要创建一个脚本,允许我在没有用户交互的情况下访问我的应用程序。 for example i press a button and the script automatically log me in (i know username and password of the user).例如,我按下一个按钮,脚本会自动让我登录(我知道用户的用户名和密码)。 The user don't need to see the window where i put username and password.用户不需要看到我输入用户名和密码的 window。 Is it possible?可能吗? at the moment my login script is:目前我的登录脚本是:

const authResult = await msalClient.loginPopup(msalRequest);
localStorage.setItem('msalAccount', authResult.account.username);

// Get the user's profile from Graph
user = await getUser();
// Save the profile in session
localStorage.setItem('graphUser', JSON.stringify(user));
if(accountAttivo!=""){
setActiveUser();
}
updatePage(Views.home);

Looks like you are trying to follow the ROPC flow as you are using username and password credentials in your script, access token must be fetched from AAD before we call getUser().看起来您正在尝试遵循 ROPC 流程,因为您在脚本中使用用户名和密码凭据,在我们调用 getUser() 之前,必须从 AAD 获取访问令牌。 To fetch any details using graph api we need to have access token please go through the article which helps you more in understanding.要使用图 api 获取任何详细信息,我们需要获得访问令牌,请通过文章go 帮助您更多地理解。

Let's say we wrote a function called getAccessToken() to fetch the token.假设我们编写了一个名为 getAccessToken() 的 function 来获取令牌。 Once the token fetched and is saved in the sessions, following code can be used in getUser() to fetch the user profile.获取令牌并保存在会话中后,可以在 getUser() 中使用以下代码来获取用户配置文件。

const options = {
    authProvider,
};
const client = Client.init(options);
let res = await client.api('/me')
    .get();

Calling both the functions getAccessToken() and getUser() in the code flow of the button click event should bypass the user interaction with the application to enter credentials.在按钮单击事件的代码流中调用函数 getAccessToken() 和 getUser() 应该绕过用户与应用程序的交互以输入凭据。

NOTE : Microsoft does not recommend to user ROPC flow.注意:Microsoft 不建议用户使用 ROPC 流程。 This most scenarios, more secure alternatives are available and recommended.在大多数情况下,可以使用并推荐更安全的替代方案。 This flow requires a very high degree of trust in application, and carries risks which are not present in other flows.此流程需要对应用程序具有非常高的信任度,并带有其他流程中不存在的风险。 You should only use this flow when other more secure flows can't be used.只有在无法使用其他更安全的流程时,您才应该使用此流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM