Sub topic 5
Setting Fine-Grained Permissions
This chapter covers the permission model of fine-grained personal access tokens. It assumes you can already create a fine-grained token and choose its resource owner and repositories, and that you can make REST calls with a bearer token from a terminal.
Permissions are separate from repository selection
Choosing repositories answers the question "where can this token act?" Permissions answer "what can it do there?" A fine-grained token needs both. A token with access to one repository and no permissions can read only public data.
Permissions fall into two groups:
- Repository permissions apply to the repositories you selected. Examples are Contents, Issues, Pull requests, Metadata, Actions and Workflows.
- Organization permissions apply to the organization as a whole, such as Members or Administration. They appear only when the resource owner is an organization.
- Account permissions apply to your own user account, such as Email addresses. They appear only when the resource owner is your user account.
Access levels
Most permissions offer one of three levels: no access, read-only, or read and write. Some permissions have a single level, because the action has only one meaning.
| Level | What it allows | Typical use |
|---|---|---|
| No access | Nothing for that resource | Default for every permission |
| Read-only | GET requests and clones | Reporting, auditing, CI that only fetches code |
| Read and write | Reads plus create, update and delete | Bots that open issues or push commits |
Read and write includes read. You never need to grant both.
Metadata is always granted
When you select any permission, GitHub adds read-only Metadata. This lets the token fetch basic repository facts such as name, collaborators and topics. You cannot remove it, so do not assume a token with "Issues: read" can touch only issues.
Mapping a task to permissions
Work backwards from the API endpoints you call. Each endpoint's documentation lists the fine-grained permission it requires. For example:
- Listing issues needs Issues: read.
- Creating a pull request needs Pull requests: write.
- Reading a file with the contents endpoint needs Contents: read.
- Editing workflow files needs Workflows: write, which is separate from Contents.
The last check matters. A token can never exceed what its owner can do. Granting a permission does not create access that you lack.
Testing the permissions you chose
Start with the smallest set you expect to need, run the call, and add a permission only when the API tells you it is missing. This script checks whether a token can read issues and then tries a write.
curl -s -o /dev/null -w "read issues: %{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/REPO/issues
curl -s -o /dev/null -w "create issue: %{http_code}\n" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{"title":"Permission test"}' \
https://api.github.com/repos/OWNER/REPO/issuesWith Issues: read, the first call returns 200 and the second returns 403. Note that the second call creates a real issue if it succeeds, so test in a scratch repository.
Common errors
- 403 "Resource not accessible by personal access token." The token lacks a permission or level. Open the token settings and raise the specific permission.
- Permission change seems to have no effect. Editing a token's permissions applies to the same token string. If the owner is an organization that requires approval, the change can return the token to a pending state.
- Endpoint does not support fine-grained tokens. A few endpoints still require a classic token. The endpoint documentation says so.
Exercise
A reporting script uses a fine-grained token with Issues: read on one repository. It fails with 403 when it tries to add a label to an issue. Which permission and level should you grant, and why is that better than switching to a classic token?
Show answer
Grant Issues: read and write (labels on issues fall under Issues write). This keeps the token limited to the selected repository and to one permission, while a classic token with the repo scope would give access to all repositories the owner can reach and to many unrelated actions.
Exercise
You select only "Pull requests: read" on a token. Can it read repository collaborators' basic metadata, and can you remove that ability?
Show answer
Yes. GitHub automatically adds read-only Metadata to any token with permissions, and you cannot remove it.