Sub topic 3
Fine-grained personal access tokens
This chapter assumes you know how GitHub repositories and organizations are structured, can send HTTP requests from a terminal, and know what a bearer token is. Here you will create a fine-grained personal access token (PAT) and use it to call the REST API.
What a fine-grained PAT is
A fine-grained PAT is a token that acts as you but can only reach what you choose at creation time. A classic PAT uses broad OAuth scopes such as repo, which grants read and write access to every repository you can reach. A fine-grained PAT replaces that with three limits:
- One resource owner: your user account or a single organization.
- A set of repositories: all of them, only public ones, or a list you select.
- A set of permissions, each set to no access, read, or read and write.
A leaked classic token exposes everything you can touch. A leaked fine-grained token exposes only the repositories and permissions you picked, and it expires.
| Property | Classic PAT | Fine-grained PAT |
|---|---|---|
| Scope model | Broad OAuth scopes | Per-permission access levels |
| Repository limit | All repositories you can access | Selected repositories only |
| Owner | Your account | One user or organization |
| Expiration | Optional | Required by default |
How the pieces fit together
The token never gives you more than your own account has. It is the intersection of your access and the token's limits.
Creating a token
- Open Settings, then Developer settings, then Personal access tokens, then Fine-grained tokens.
- Choose Generate new token and give it a name that describes its job, such as
ci-read-issues. - Pick an expiration date. Shorter is safer.
- Choose the resource owner. If it is an organization, the organization may require an owner to approve the token before it works.
- Select the repositories. Prefer "Only select repositories" over "All repositories".
- Under Permissions, add only what the job needs. For example, set Issues to read-only if the script only lists issues.
- Generate the token and copy it. GitHub shows it only once.
Calling the API
Store the token in an environment variable instead of typing it into a command. Then send it in the Authorization header.
export GITHUB_TOKEN="paste-your-token-here"
curl -L \
-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/REPOReplace OWNER and REPO with a repository you selected. The same request works with an installation access token from a GitHub App, which later chapters cover.
Common errors
- 401 Unauthorized: The token is wrong, expired, or revoked. Check the variable and the expiration date.
- 404 Not Found on a repository you can see: The token does not include that repository. GitHub returns 404 instead of 403 for private resources to avoid revealing that they exist.
- 403 Forbidden: The token reaches the repository but lacks the permission the endpoint needs. Check the endpoint's documentation for the required permission and raise it only as far as needed.
- Works on a personal repository, fails on an organization one: The resource owner was set to your account, or the organization has not approved the token.
- Some endpoints are unsupported: A few endpoints and features still do not accept fine-grained PATs. In that case, check the endpoint's documentation before falling back to a classic token.
Limits to keep in mind
A fine-grained PAT is tied to a person. If that person leaves the organization, the token stops working, and any automation that relied on it breaks. For shared automation that should outlive one person, a GitHub App is the better fit, which is where the next chapters go.
Exercise
A script must list open issues in one repository, acme/widgets, and never change anything. Which resource owner, repository selection and permission would you choose when creating a fine-grained PAT?
Show answer
Set the resource owner to the acme organization, select only the acme/widgets repository, and grant Issues read-only access. Metadata read access is added automatically. Choose a short expiration, and have an organization owner approve the token if the organization requires approval.
Exercise
Your token returns 404 for a private repository that you can open in the browser. What is the most likely cause, and how do you fix it?
Show answer
The token's repository selection or resource owner does not include that repository. Edit the token, or create a new one, so that it covers the repository under the correct owner. Confirm that the organization has approved it if approval is required.