我正在研究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);代码示例将非常有用。
发布于 2017-08-17 06:55:13
此代码仅为您提供用户的token。为了获得有关用户的信息,您需要调用Microsoft Graph API。您可以在该站点上找到全套文档。
如果您只是为了获取配置文件信息而进行身份验证,我建议您查看Enable single sign-on for Office Add-ins (preview)。这是为用户获取访问令牌的一种清晰得多的方法。目前它还在预览阶段,所以它的可行性将取决于你计划在哪里部署你的插件。
发布于 2018-03-27 20:37:31
一旦你有了微软的令牌,你就可以向https://graph.microsoft.com/v1.0/me/发送一个请求来获取用户信息。此请求必须有一个authorization头部,其中包含您之前获得的令牌。
下面是一个使用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
};https://stackoverflow.com/questions/45723722
复制相似问题