AppCredential class

Represent Microsoft 365 tenant identity, and it is usually used when user is not involved like time-triggered automation job.

Example

loadConfiguration(); // load configuration from environment variables
const credential = new AppCredential();

Remarks

Only works in in server side.

Constructors

AppCredential(AuthenticationConfiguration)

Constructor of AppCredential.

AppCredential(OnBehalfOfCredentialAuthConfig)

Constructor of AppCredential.

Methods

getToken(string | string[], GetTokenOptions)

Get access token for credential.

Example

await credential.getToken(["User.Read.All"]) // Get Graph access token for single scope using string array
await credential.getToken("User.Read.All") // Get Graph access token for single scope using string
await credential.getToken(["User.Read.All", "Calendars.Read"]) // Get Graph access token for multiple scopes using string array
await credential.getToken("User.Read.All Calendars.Read") // Get Graph access token for multiple scopes using space-separated string
await credential.getToken("https://graph.microsoft.com/User.Read.All") // Get Graph access token with full resource URI
await credential.getToken(["https://outlook.office.com/Mail.Read"]) // Get Outlook access token

Constructor Details

AppCredential(AuthenticationConfiguration)

Constructor of AppCredential.

new AppCredential(authConfig: AuthenticationConfiguration)

Parameters

authConfig
AuthenticationConfiguration

The authentication configuration. Use environment variables if not provided.

Remarks

Only works in in server side.

AppCredential(OnBehalfOfCredentialAuthConfig)

Constructor of AppCredential.

new AppCredential(authConfig: OnBehalfOfCredentialAuthConfig)

Parameters

authConfig
OnBehalfOfCredentialAuthConfig

The authentication configuration.

Remarks

Only works in in server side.

Method Details

getToken(string | string[], GetTokenOptions)

Get access token for credential.

Example

await credential.getToken(["User.Read.All"]) // Get Graph access token for single scope using string array
await credential.getToken("User.Read.All") // Get Graph access token for single scope using string
await credential.getToken(["User.Read.All", "Calendars.Read"]) // Get Graph access token for multiple scopes using string array
await credential.getToken("User.Read.All Calendars.Read") // Get Graph access token for multiple scopes using space-separated string
await credential.getToken("https://graph.microsoft.com/User.Read.All") // Get Graph access token with full resource URI
await credential.getToken(["https://outlook.office.com/Mail.Read"]) // Get Outlook access token
function getToken(scopes: string | string[], options?: GetTokenOptions): Promise<null | AccessToken>

Parameters

scopes

string | string[]

The list of scopes for which the token will have access.

options

GetTokenOptions

The options used to configure any requests this TokenCredential implementation might make.

Returns

Promise<null | AccessToken>

Access token with expected scopes. Throw error if get access token failed.