OfficeRuntime.Auth interface
Interface that contains authorization related APIs.
Remarks
The methods in this interface are equivalent to those in the Office.auth interface. If new authentication types are added in the future, they will only be added to the Office.auth
interface. For simplicity, the code examples throughout the documentation use Office.auth
.
Examples
// Get the auth context object and use it to get an
// access token.
const authContext = OfficeRuntime.context.auth;
const accessToken = authContext.getAccessTokenAsync();
Methods
get |
Calls the Azure Active Directory V 2.0 endpoint to get an access token to your add-in's web application. Enables add-ins to identify users. Server-side code can use this token to access Microsoft Graph for the add-in's web application by using the "on behalf of" OAuth flow. This API requires a single sign-on configuration that bridges the add-in to an Azure application. Office users sign-in with Organizational Accounts and Microsoft Accounts. Microsoft Azure returns tokens intended for both user account types to access resources in the Microsoft Graph. |
Method Details
getAccessToken(options)
Calls the Azure Active Directory V 2.0 endpoint to get an access token to your add-in's web application. Enables add-ins to identify users. Server-side code can use this token to access Microsoft Graph for the add-in's web application by using the "on behalf of" OAuth flow. This API requires a single sign-on configuration that bridges the add-in to an Azure application. Office users sign-in with Organizational Accounts and Microsoft Accounts. Microsoft Azure returns tokens intended for both user account types to access resources in the Microsoft Graph.
getAccessToken(options?: AuthOptions): Promise<string>;
Parameters
- options
- OfficeRuntime.AuthOptions
Optional. Accepts an AuthOptions
object to define sign-on behaviors.
Returns
Promise<string>
Promise to the access token.
Remarks
Applications: Excel, Outlook, PowerPoint, Word
Important:
In Outlook, this API isn't supported if you load an add-in in an Outlook.com or Gmail mailbox.
In Outlook on the web, this API isn't supported if you use the Safari browser. This results in error 13001 ("The user is not signed into Office").
In Outlook on the web, if you use the displayDialogAsync method to open a dialog, you must close the dialog before you can call
getAccessToken
.
Examples
async function getUserData() {
try {
let userTokenEncoded = await OfficeRuntime.auth.getAccessToken();
let userToken = jwt_decode(userTokenEncoded); // Using the https://www.npmjs.com/package/jwt-decode library.
console.log(userToken.name); // user name
console.log(userToken.preferred_username); // email
console.log(userToken.oid); // user id
}
catch (exception) {
if (exception.code === 13003) {
// SSO is not supported for domain user accounts, only
// Microsoft 365 Education or work account, or a Microsoft account.
} else {
// Handle error
}
}
}
Office Add-ins