Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user info using Office-js-helper's Authentication?

I am working on Excel Web Add-In. I am using OfficeDev/office-js-helpers library for authenticating user. Following code is working fine. But I don't know how to get user's email, user name etc.

Is there any function available in OfficeDev/office-js-helpers through which I can get user info ?

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);

Code sample will be much helpful.

like image 912
user3881465 Avatar asked Jan 18 '26 10:01

user3881465


2 Answers

This code only provides you the token for the user. In order to obtain information about the user, you'll need to make calls into Microsoft Graph API. You can find a full set of documentation on that site.

If you're only authenticating in order to get profile information, I'd recommend looking at Enable single sign-on for Office Add-ins (preview). This is a much cleaner method of obtaining an access token for a user. It is still in preview at the moment so it's feasibility will depend on where you're planning to deploy your add-in.

like image 189
Marc LaFleur Avatar answered Jan 21 '26 00:01

Marc LaFleur


Once you have the Microsoft token, you can send a request to https://graph.microsoft.com/v1.0/me/ to get user information. This request must have an authorization header containing the token you got previously.

Here is an example using 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
};
like image 42
blandine Avatar answered Jan 20 '26 22:01

blandine