Documentation

Act as an Organization

Exchange a user token for an org-scoped token that acts on behalf of an organization.

This page exchanges a user's access token for an org-scoped token, which acts on behalf of one organization. A user token acts only on the user's own resources. Endpoints that manage an organization's memberships, activities, rewards or webhooks need an org-scoped token, and are labelled "Requires an org-scoped token" in the API Reference.

The exchange is the OAuth 2.0 token exchange grant (RFC 8693).

Requirements

  • The token exchange grant enabled on your app. A new app does not have it. Turn it on from the app's page under Developer → Apps.
  • A user access token issued to the same app, from Sign In a User. A token issued to a different app is refused.
  • The user on the organization's staff, as owner, manager or member.

Exchange the token

curl -X POST "https://api.davi.social/oauth2/token" \
  -u "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
  -d "subject_token=USER_ACCESS_TOKEN" \
  -d "subject_token_type=urn:ietf:params:oauth:token-type:access_token" \
  -d "resource=org:ORG_SLUG"

resource names the target organization as org:<organization_slug>.

{
  "access_token": "eyJhbGciOi...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "...",
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token"
}

Send the returned access_token as the bearer token on organization endpoints.

Things to know

  • The scopes are narrowed to the user's role. The new token carries the user token's scopes that the user's role in this organization allows. Read scope from the response: a member gets fewer than an owner.
  • Endpoints check the role as well as the scope. The token carries the user's role, and a request the role does not permit is refused even with the right scope.
  • No refresh token is issued. When the token expires, refresh the user token and exchange again.
  • The exchange fails if the user is not on the organization's staff, or if the user token has been revoked.

To exchange with nobody signed in, from a scheduled job or a form handler, see Automate for Your Organization.

Next