Sub topic 1
How API authentication with tokens works
Prerequisites
This chapter assumes you know the following:
- What GitHub repositories and organizations are.
- How an HTTP request is built: method, URL, headers, status code.
- How to use a terminal and set environment variables.
- Roughly what a bearer token is. The chapter recaps it.
You do not need an account with any special plan. A free GitHub account is enough for the examples.
Authentication and authorization
Every request to the GitHub REST API answers two questions in order.
- Authentication: who is making this request?
- Authorization: is that caller allowed to do what the request asks?
A token answers the first question. It is a secret string that stands in for an identity. GitHub looks the token up, finds who or what it belongs to, and then checks what that token is permitted to do. The permission check is the part this tutorial cares about. Two tokens for the same user can be allowed very different things.
The bearer token model
GitHub uses bearer authentication. Whoever holds, or "bears," the token is treated as its owner. There is no second proof, such as a signature on each request. This has one practical consequence: anyone who copies the token can act with all of its access until it expires or is revoked.
The token travels in the Authorization HTTP header. Here is a request that reads a repository's details.
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 real account and repository name. Set GITHUB_TOKEN in your shell first, for example with export GITHUB_TOKEN=.... The command reads the value from the environment, so the secret never appears in your command history or in a script.
The headers do the following.
| Header | Purpose |
|---|---|
| `Accept` | Asks for the standard GitHub JSON response format. |
| `Authorization` | Carries the token after the word `Bearer`. |
| `X-GitHub-Api-Version` | Pins the API version so later changes do not alter responses unexpectedly. |
The -L flag tells curl to follow redirects. GitHub redirects some requests, for example when a repository has been renamed.
The same request works whichever kind of token you hold. A fine-grained personal access token and an installation access token are both sent this way. Only the access they carry differs.
What happens on the server
The next diagram shows the path of a request.
Reading the common errors
Most early problems come from these status codes.
| Status | Usual cause | What to check |
|---|---|---|
| 401 | The token is missing, mistyped, expired or revoked. | Print the length of `$GITHUB_TOKEN` to confirm it is set. Check the expiration date. |
| 403 | The token is valid but lacks a permission, or you hit a rate limit. | Read the response body and headers. Compare the token's permissions with the endpoint's documentation. |
| 404 | The resource does not exist, or the token cannot see it. | Confirm the repository is among those the token can access. |
The 404 case surprises many people. For private resources, GitHub often answers 404 instead of 403 so that it does not reveal that the resource exists. If a repository you own returns 404, suspect the token's access before you suspect the URL.
Why the kind of token matters
The request you send is identical for every token type. What differs is how much damage a leaked token can do, and that depends on how narrowly the token was scoped. The tutorial covers three groups:
- Classic personal access tokens, which use broad scopes.
- Fine-grained personal access tokens, limited to chosen repositories and specific permissions.
- GitHub App tokens, issued to an app that was installed on specific accounts.
The following chapters take them in that order, and show why narrower access is safer.
Exercise
You send a request with a valid fine-grained token to a private repository the token was not granted access to. Which status code might you see, and why might it not be 403?
Show answer
You might see 404. GitHub often returns 404 for private resources the caller cannot access, so that it does not confirm the resource exists. A 401 would mean the token itself was invalid, which is not the case here.
Exercise
Why is it safer to read the token from an environment variable such as $GITHUB_TOKEN than to paste it into the curl command?
Show answer
A pasted token is stored in shell history and may be copied into scripts, screenshots or logs. An environment variable keeps the secret out of the command text, so the command can be shared or saved without exposing it.