【问题标题】:How to get user info using Office-js-helper's Authentication?如何使用 Office-js-helper 的身份验证获取用户信息?
【发布时间】:2018-01-25 04:43:10
【问题描述】:

我正在处理Excel Web Add-In。我正在使用OfficeDev/office-js-helpers 库来验证用户。以下代码工作正常。但我不知道如何获取用户的电子邮件、用户名等。

OfficeDev/office-js-helpers 有没有可以获取用户信息的功能?

if (OfficeHelpers.Authenticator.isAuthDialog()) {
  return;
}

var authenticator = new OfficeHelpers.Authenticator();

// register Microsoft (Azure AD 2.0 Converged auth) endpoint using
authenticator.endpoints.registerMicrosoftAuth('clientID');

// for the default Microsoft endpoint
authenticator
    .authenticate(OfficeHelpers.DefaultEndpoints.Microsoft)
    .then(function (token) { 
    /* My code after authentication and here I need user's info */ })
    .catch(OfficeHelpers.Utilities.log);

代码示例会很有帮助。

【问题讨论】:

    标签: javascript authentication office-js office-addins exceljs


    【解决方案1】:

    获得 Microsoft 令牌后,您可以向 https://graph.microsoft.com/v1.0/me/ 发送请求以获取用户信息。此请求必须有一个包含您之前获得的令牌的授权标头。

    这是一个使用 axios 的示例:

    const config = { 'Authorization': `Bearer ${token.access_token}` };
    axios.get(`https://graph.microsoft.com/v1.0/me/`, {
        headers: config
    }).then((data)=> {
        console.log(data); // data contains user information
    };
    

    【讨论】:

      【解决方案2】:

      此代码仅为用户提供token。为了获取有关用户的信息,您需要拨打Microsoft Graph API。您可以在该站点上找到完整的文档集。

      如果您只是为了获取个人资料信息而进行身份验证,我建议您查看Enable single sign-on for Office Add-ins (preview)。这是为用户获取访问令牌的更简洁的方法。它目前仍处于预览阶段,因此其可行性将取决于您计划在何处部署加载项。

      【讨论】:

      • 感谢您的回答。没有任何干净的代码示例可用于在OfficeDev/office-js-helpers 中获取用户的基本信息吗?您提供的链接有更多的理论概念,但很少有代码示例
      • @user3881465 你可以在这里获取代码示例:docs.microsoft.com/en-us/office/dev/add-ins/quickstarts/…
      猜你喜欢
      • 2019-08-31
      • 2011-10-06
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多