# `connection.on_behalf_of_missing`

A call through a connection whose credential is an OAuth grant doesn't say whose grant it uses with `on_behalf_of:`.

Area 11, [External services](/docs/external-services.md); not declinable; the next step is usually the agent's.

## Why Jadpo refuses it

A connection whose credential is an OAuth grant holds one grant for each
record of its owner entity that connected, and acts at the provider with
one of them at a time. Nothing else can say which: a schedule or a
subscriber runs with no caller, and an action may act for an organisation,
or for a user other than the one who called it. So every call names the
record whose grant it uses, `on_behalf_of:`, an `id` of the owner's entity.

## What it looks like










```jadpo
use accounts/entities { User }

application {
    connections: {
        calendar: {
            package: GoogleCalendar.v3
            domains: [accounts]
            environment: config.calendar_environment
            credential: OAuthGrant {
                owner: User
                client_id: config.google_client_id
                client_secret: config.google_client_secret
                scopes: ["https://www.googleapis.com/auth/calendar.events"]
                return_to: "/settings/integrations"
            }
        }
    }
    grants: {
        encryption_key: config.grant_encryption_key
    }
}

principal {
    user: {
        subject: User.email
    }
}
```

The `accounts` domain adds an event to a user's calendar without saying
whose.

domains/accounts/actions/add_event.jadpo:

```jadpo
use app { calendar }
use shared/providers/google_calendar

/// Adds an event to a user's calendar.
action add_event(event: GoogleCalendar.NewEvent) -> GoogleCalendar.AddedEvent {
    return calendar.events.insert(event)
}
```

```text
error: `calendar.events.insert` doesn't say whose grant it uses
 --> domains/accounts/actions/add_event.jadpo:6:12
  |
6 |     return calendar.events.insert(event)
  |            ^^^^^^^^^^^^^^^^^^^^^^ no `on_behalf_of:`
  reason: `calendar` acts at the provider with one owner's grant at a time, and nothing else says which: not the caller, since a schedule or a subscriber has none, and an action may act for someone other than who called it.
  next: Name the `User` whose grant the call uses: `calendar.events.insert(..., on_behalf_of: <User.id>)` (agent)
  rule: connection.on_behalf_of_missing
  help: https://jadpo.dev/docs/errors/connection.on_behalf_of_missing

check failed: 1 error, 11 files
```

`(agent)` at the end of the `next:` line means your coding agent can make
the fix itself: the call has one owner whose grant it uses, which the
action already knows or is given.

## How to fix it

**If the call acts for the signed-in user**, take the caller and pass
their `id`:

```diff
-action add_event(event: GoogleCalendar.NewEvent) -> GoogleCalendar.AddedEvent {
-    return calendar.events.insert(event)
+action add_event(me: Principal.user, event: GoogleCalendar.NewEvent) -> GoogleCalendar.AddedEvent {
+    return calendar.events.insert(event, on_behalf_of: me.id)
```

Calling with a grant isn't built yet, so for now this call is `toolchain.not_built_yet`.

The event then goes into the signed-in user's own calendar, through the
grant they connected.

**If it acts for someone else**, such as a booking's organiser, pass that
record's `id` instead:

```diff
-    return calendar.events.insert(event)
+    return calendar.events.insert(event, on_behalf_of: booking.organiser_id)
```

The call then uses the organiser's grant, whoever called the action, which
a schedule or a subscriber can do too.

## Related

- [OAuth grants](/docs/oauth-grants.md)
- [Calling with a grant](/docs/oauth-grants.md#calling-with-a-grant)
- [`connection.scope_not_requested`](/docs/errors/connection.scope_not_requested.md)
