3

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.

user3881465
  • 239
  • 2
  • 5
  • 19

2 Answers2

4

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.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 1
    Thanks for answer. Is not there any clean code sample available for getting user's basic info in `OfficeDev/office-js-helpers `? Links you provided has theory concept more but very less code sample – user3881465 Aug 16 '17 at 23:21
  • @user3881465 you can get code samples here: https://learn.microsoft.com/en-us/office/dev/add-ins/quickstarts/sso-quickstart – João Pimentel Ferreira Aug 21 '20 at 17:11
2

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
};
Yoh Deadfall
  • 2,711
  • 7
  • 28
  • 32
blandine
  • 93
  • 1
  • 10