Sub topic 4
Choosing a resource owner and repositories
This chapter assumes you know what a bearer token is, can call the GitHub REST API from a terminal, and understand the difference between a personal account and an organization. Here you will learn the first two choices you make when creating a fine-grained personal access token: who owns the resources, and which repositories the token can reach.
The resource owner
A fine-grained token is bound to exactly one resource owner. The resource owner is the user account or organization that owns the repositories the token will touch. You pick it on the token creation page, and you cannot change it afterward.
This is a major difference from a classic token. A classic token with the repo scope works on every repository you can reach, across your account and every organization you belong to. A fine-grained token works only inside the owner you chose. To work with repositories under two owners, you create two tokens.
Some organizations do not appear in the owner list, or appear with a note that approval is needed. An organization can block fine-grained tokens entirely, or require an owner to approve each token before it works. The lifecycle chapter covers those policies.
Repository selection
After you pick the owner, you choose one of three repository access modes.
| Mode | What the token can reach | Typical use |
|---|---|---|
| Public repositories | Read-only access to public data. No write access and no private repositories. | Scripts that read open source projects and need a higher rate limit |
| All repositories | Every repository the owner has, including ones created later | Organization-wide tooling that must cover new repositories |
| Only select repositories | The repositories you list, up to 50 | Most automation. A deploy script, a release bot, a CI helper |
Prefer Only select repositories. A token that leaks can then expose only the repositories on its list, not everything the owner has.
The token's reach is also capped by your own access. A token acts as you, so it can never do more than your account can do in that repository. If you lose access to a repository, the token loses it too.
How the two choices combine
A request succeeds only when every layer allows it. Your account must have access, the resource owner must match, the repository must be selected, and the permission must be granted for the action.
Permissions are the subject of the next chapter. For now, note that choosing a repository does not grant any ability on it. A new token with repositories selected but no permissions can read only public information.
Verifying what a token can reach
Test the token instead of trusting the settings page. Call a repository it should reach, then one it should not.
# A repository you selected: expect 200
curl -s -o /dev/null -w "%{http_code}\n" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/SELECTED_REPO
# A private repository you did not select: expect 404
curl -s -o /dev/null -w "%{http_code}\n" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/OTHER_PRIVATE_REPOGitHub returns 404 instead of 403 for private repositories a token cannot see. This hides whether the repository exists. If you get a 404 on a repository you expected to reach, check the resource owner and the repository list first.
Common mistakes
- Creating the token under your personal account when the repository belongs to an organization. The token then sees none of the organization's repositories.
- Choosing All repositories for convenience, then forgetting that new repositories are covered.
- Expecting a repository selection to change later. You can edit the list, but not the owner.
- Assuming a
404means the repository is missing, when it often means the token cannot see it.
Exercise
You need a script that reads issues from two private repositories owned by the organization acme, and from one private repository owned by your personal account. How many fine-grained tokens do you need, and how should you select repositories for each?
Show answer
Two tokens. A fine-grained token has one resource owner, so one token has acme as owner and the other has your personal account. For the acme token, choose Only select repositories and list the two repositories. For the personal token, choose Only select repositories and list the one repository. Remember that acme may require approval before its token works.