Sub topic 4
Choosing a resource owner and repositories
This chapter covers the first two choices on the fine-grained token form: who owns the resources, and which repositories the token can reach. It assumes you can create a fine-grained personal access token (PAT) in the GitHub settings UI. It also assumes you know the difference between a personal account and an organization, and that you can call the REST API with curl and an Authorization: Bearer header.
Why these two choices come first
A classic token carries your whole reach. If you can see 200 repositories across five organizations, a classic token with the repo scope can touch all of them. A fine-grained token reverses that default. It starts with no access, and you add access in layers:
- The resource owner decides which account the token can work inside.
- The repository access setting decides which of that owner's repositories the token can reach.
- The permissions (the next chapter) decide what the token can do in those repositories.
The token can only do something if all three layers allow it. The token also never exceeds what your own account can do. Granting a permission your account lacks has no effect.
The resource owner
The resource owner is the user account or organization that owns the repositories the token will access. You pick it in a drop-down when you create the token. The drop-down lists your own account and the organizations you belong to. Organizations where you are an outside collaborator also appear.
Three rules follow from this design:
- A token has exactly one resource owner. It cannot reach repositories owned by two different accounts.
- You cannot change the resource owner after creation. To work in a second organization, create a second token.
- The owner you pick determines the rules that apply. Your personal account has no approval step. An organization can require approval or restrict fine-grained tokens entirely, as covered in a later chapter.
One token per owner can look like extra work. It makes a leaked token cheaper, because the damage stays inside one account.
What the owner looks like in practice
Suppose you own alice/notes and you belong to the acme organization, which has acme/api and acme/web. These are the possible reaches:
| Resource owner | Repositories the token can ever reach |
|---|---|
| `alice` | `alice/notes` and your other personal repositories |
| `acme` | `acme/api`, `acme/web`, and any other `acme` repositories you can access |
A token owned by alice returns an error for acme/api. A token owned by acme returns an error for alice/notes.
Repository access
After choosing the owner, you choose one of three repository access modes.
| Mode | What it covers | Typical use |
|---|---|---|
| Public repositories | Read-only access to public resources. No private repository is reachable. | Reading public data with a higher rate limit than anonymous calls |
| All repositories | Every repository the owner has, including ones created later | Personal automation across all your own projects |
| Only select repositories | A list you choose, up to 50 repositories | Most scripts and CI jobs |
Public repositories
This mode gives read-only access to public data. It is useful when a script only reads public repositories. Because it carries no write access and no private access, it is the safest mode. If you pick it, the permission section does not apply in the usual way, since nothing can be written.
All repositories
This mode includes repositories created after the token. A new private repository appears inside the token's reach the moment it is created, without any action from you. That is convenient, and it is also a risk. A token you created for three projects quietly covers the fourth one you create next year. Use this mode only when the automation really is meant to cover everything the owner has.
For an organization, "all repositories" means all repositories in that organization that your account can access. It does not mean everything in the organization.
Only select repositories
This mode is the default choice for most work. You pick repositories by name, and the list does not grow on its own. The limit is 50 repositories per token. If you need more than that, the job probably belongs to a GitHub App, which is covered in the next sections of the tutorial.
You can change the selected repositories after creation by editing the token. You can add or remove repositories at any time, so start with the smallest list that works and add to it when you need to.
The metadata permission
Every fine-grained token that can reach a repository gets read-only access to that repository's metadata. This includes the repository name, its visibility, and its collaborators list. You cannot remove this. It is why GET /repos/OWNER/REPO works for any selected repository, even before you add a single permission.
How the layers combine
Seeing the boundary with a script
You can check what a token reaches before you rely on it. The first call lists the repositories that the token can access.
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/user/repos?per_page=100" \
| jq -r '.[].full_name'With "Only select repositories", the output should list only your selected repositories. The jq tool is a command-line JSON processor, and you need to have it installed.
The next script tests specific repositories and prints the HTTP status for each. It needs Node.js 18 or later, which includes fetch.
const token = process.env.GITHUB_TOKEN;
const repos = process.argv.slice(2);
if (!token || repos.length === 0) {
console.error("Usage: GITHUB_TOKEN=... node check.mjs owner/repo [owner/repo ...]");
process.exit(1);
}
for (const repo of repos) {
const res = await fetch(`https://api.github.com/repos/${repo}`, {
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": "2022-11-28",
},
});
console.log(`${repo}: ${res.status}`);
}Save it as check.mjs and run GITHUB_TOKEN=... node check.mjs alice/notes acme/api. A token owned by alice and limited to alice/notes should return 200 for the first repository. For acme/api, it should return 404.
Reading the errors
The status codes are the main way you learn that a boundary was hit, so it helps to read them correctly.
- 404 Not Found for a private repository usually means the token cannot see it. GitHub returns 404 instead of 403 so that it does not confirm the repository exists. Check the owner and the repository list first.
- 403 Forbidden usually means the token can see the repository but lacks a permission for the action. Check the permissions next.
- 401 Unauthorized means the token itself is invalid, expired, or revoked. The repository selection is not the cause.
Common mistakes
- Wrong resource owner. You create a token under your personal account, then call an organization repository. The token never had a path to it. Create a new token with the organization as the owner.
- Choosing "All repositories" for convenience. The token then covers future repositories. Switch to a selected list unless the broad scope is intended.
- Forgetting that the owner may need to approve. In an organization that requires approval, the token exists but works only for public data until an owner approves it. A 404 or 403 on a private repository can mean the token is still pending.
- Expecting one token to span accounts. A job that touches
alice/notesandacme/apineeds two tokens, or a different credential type altogether. - Assuming the token adds access. If you lose access to a repository, such as being removed from a team, the token loses it as well.
Choosing the narrowest setting
Use this order when you decide:
- Name the single account that owns the repositories the job touches. That is the resource owner.
- If the job only reads public data, pick "Public repositories."
- If the job touches a known, short list, pick "Only select repositories" and list them.
- Pick "All repositories" only when the job must follow new repositories automatically, and say why in the token's description.
If the job spans several owners or more than 50 repositories, a fine-grained token is the wrong tool, and a GitHub App is the better fit.
Exercise
You create a fine-grained token with the organization acme as the resource owner and "Only select repositories" set to acme/api. Your script calls GET /repos/acme/web, a private repository that you can open in your browser, and receives a 404. Give two possible causes and one fix for each.
Show answer
First, acme/web is not in the token's selected repository list. The fix is to edit the token and add acme/web. Second, the organization requires approval for fine-grained tokens and an owner has not yet approved this token, so it cannot reach private resources. The fix is to ask an organization owner to approve it. A 404 is returned instead of a 403 so that GitHub does not confirm that a private repository exists. Changing the resource owner is not a fix, because it is already correct and cannot be edited after creation.