Battle.net
OAuth 2.0 provider for Battle.net.
Also see the OAuth 2.0 guide.
Initialization
import * as arctic from "arctic";
const battlenet = new arctic.BattleNet(clientId, clientSecret, redirectURI);
Create authorization URL
import * as arctic from "arctic";
const state = arctic.generateState();
const scopes = ["openid", "wow.profile"];
const url = battlenet.createAuthorizationURL(state, scopes);
Validate authorization code
validateAuthorizationCode()
will either return an OAuth2Tokens
, or throw one of OAuth2RequestError
, ArcticFetchError
, UnexpectedResponseError
, or UnexpectedErrorResponseBodyError
. Battle.net returns an access token and the access token expiration.
import * as arctic from "arctic";
try {
const tokens = await battlenet.validateAuthorizationCode(code);
const accessToken = tokens.accessToken();
} catch (e) {
if (e instanceof arctic.OAuth2RequestError) {
// Invalid authorization code, credentials, or redirect URI
const code = e.code;
// ...
}
if (e instanceof arctic.ArcticFetchError) {
// Failed to call `fetch()`
const cause = e.cause;
// ...
}
// Parse error
}
Get user profile
Use the User Info
endpoint.
const response = await fetch("https://oauth.battle.net/userinfo", {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
const user = await response.json();