|
| 1 | +--- |
| 2 | +title: "Autodesk Platform Services" |
| 3 | +--- |
| 4 | + |
| 5 | +# Autodesk Platform Services |
| 6 | + |
| 7 | +OAuth 2.0 provider for [Autodesk Platform Services](https://aps.autodesk.com/en/docs/oauth/v2/developers_guide/overview/). |
| 8 | + |
| 9 | +Also see the [OAuth 2.0 with PKCE](/guides/oauth2-pkce) guide. |
| 10 | + |
| 11 | +## Initialization |
| 12 | + |
| 13 | +Pass the client secret for confidential clients. |
| 14 | + |
| 15 | +```ts |
| 16 | +import * as arctic from "arctic"; |
| 17 | + |
| 18 | +const autodesk = new arctic.Autodesk(clientId, clientSecret, redirectURI); |
| 19 | +const autodesk = new arctic.Autodesk(clientId, null, redirectURI); |
| 20 | +``` |
| 21 | + |
| 22 | +## Create authorization URL |
| 23 | + |
| 24 | +```ts |
| 25 | +import * as arctic from "arctic"; |
| 26 | + |
| 27 | +const state = arctic.generateState(); |
| 28 | +const codeVerifier = arctic.generateCodeVerifier(); |
| 29 | +const scopes = ["openid", "user:read", "data:read"]; |
| 30 | +const url = autodesk.createAuthorizationURL(state, codeVerifier, scopes); |
| 31 | +``` |
| 32 | + |
| 33 | +The list of scopes Autodesk Platform Services supports can be found at the [Developer's Guide/Scopes](https://aps.autodesk.com/en/docs/oauth/v2/developers_guide/scopes/) page. |
| 34 | + |
| 35 | +## Validate authorization code |
| 36 | + |
| 37 | +`validateAuthorizationCode()` will either return an [`OAuth2Tokens`](/reference/main/OAuth2Tokens), or throw one of [`OAuth2RequestError`](/reference/main/OAuth2RequestError), [`ArcticFetchError`](/reference/main/ArcticFetchError), [`UnexpectedResponseError`](/reference/main/UnexpectedResponseError), or [`UnexpectedErrorResponseBodyError`](/reference/main/UnexpectedErrorResponseBodyError). Autodesk Platform Services returns an access token, the access token expiration, and a refresh token. |
| 38 | + |
| 39 | +```ts |
| 40 | +import * as arctic from "arctic"; |
| 41 | + |
| 42 | +try { |
| 43 | + const tokens = await autodesk.validateAuthorizationCode(code, codeVerifier); |
| 44 | + const accessToken = tokens.accessToken(); |
| 45 | + const accessTokenExpiresAt = tokens.accessTokenExpiresAt(); |
| 46 | + const refreshToken = tokens.refreshToken(); |
| 47 | +} catch (e) { |
| 48 | + if (e instanceof arctic.OAuth2RequestError) { |
| 49 | + // Invalid authorization code, credentials, or redirect URI |
| 50 | + const code = e.code; |
| 51 | + // ... |
| 52 | + } |
| 53 | + if (e instanceof arctic.ArcticFetchError) { |
| 54 | + // Failed to call `fetch()` |
| 55 | + const cause = e.cause; |
| 56 | + // ... |
| 57 | + } |
| 58 | + // Parse error |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Refresh access tokens |
| 63 | + |
| 64 | +Use `refreshAccessToken()` to get a new access token using a refresh token. Autodesk Platform Services returns the same values as during the authorization code validation. This method also returns `OAuth2Tokens` and throws the same errors as `validateAuthorizationCode()` |
| 65 | + |
| 66 | +```ts |
| 67 | +import * as arctic from "arctic"; |
| 68 | + |
| 69 | +try { |
| 70 | + // Pass an empty `scopes` array to keep using the same scopes. |
| 71 | + const tokens = await autodesk.refreshAccessToken(refreshToken, scopes); |
| 72 | + const accessToken = tokens.accessToken(); |
| 73 | + const accessTokenExpiresAt = tokens.accessTokenExpiresAt(); |
| 74 | +} catch (e) { |
| 75 | + if (e instanceof arctic.OAuth2RequestError) { |
| 76 | + // Invalid authorization code, credentials, or redirect URI |
| 77 | + } |
| 78 | + if (e instanceof arctic.ArcticFetchError) { |
| 79 | + // Failed to call `fetch()` |
| 80 | + } |
| 81 | + // Parse error |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Revoke tokens |
| 86 | + |
| 87 | +Use `revokeToken()` to revoke a token. You need to specify wether the token is an `access_token` or a `refresh_token`. This can throw the same errors as `validateAuthorizationCode()`. |
| 88 | + |
| 89 | +```ts |
| 90 | +try { |
| 91 | + await autodesk.revokeToken(token, token_type); |
| 92 | +} catch (e) { |
| 93 | + if (e instanceof arctic.OAuth2RequestError) { |
| 94 | + // Invalid authorization code, credentials, or redirect URI |
| 95 | + } |
| 96 | + if (e instanceof arctic.ArcticFetchError) { |
| 97 | + // Failed to call `fetch()` |
| 98 | + } |
| 99 | + // Parse error |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## OpenID Connect |
| 104 | + |
| 105 | +Use OpenID Connect with the `openid` scope to get the user's profile with an ID token or the [`userinfo` endpoint](https://aps.autodesk.com/en/docs/profile/v1/reference/profile/oidcuserinfo/). Arctic provides [`decodeIdToken()`](/reference/main/decodeIdToken) for decoding the token's payload. |
| 106 | + |
| 107 | +See the endpoint documentation for the token claims. |
| 108 | + |
| 109 | +```ts |
| 110 | +const scopes = ["openid"]; |
| 111 | +const url = autodesk.createAuthorizationURL(state, codeVerifier, scopes); |
| 112 | +``` |
| 113 | + |
| 114 | +```ts |
| 115 | +import * as arctic from "arctic"; |
| 116 | + |
| 117 | +const tokens = await autodesk.validateAuthorizationCode(code, codeVerifier); |
| 118 | +const idToken = tokens.idToken(); |
| 119 | +const claims = arctic.decodeIdToken(idToken); |
| 120 | +``` |
| 121 | + |
| 122 | +```ts |
| 123 | +const response = await fetch("https://api.userprofile.autodesk.com/userinfo", { |
| 124 | + headers: { |
| 125 | + Authorization: `Bearer ${accessToken}` |
| 126 | + } |
| 127 | +}); |
| 128 | +const user = await response.json(); |
| 129 | +``` |
0 commit comments