Coding

GitHub OAuth App: How to Create One and Use It to Sign In

A GitHub OAuth app lets your site or tool act on behalf of a GitHub user, for example to sign them in or read their repositories, without ever seeing their password. You create one under Settings, Developer settings, OAuth apps, then send users through GitHub's authorize URL and exchange the returned code for an access token.

This guide covers registering the app, the three requests that make up the web application flow, which scopes to ask for, and when a different kind of GitHub integration fits better. Details are from GitHub's documentation as of November 2026. For the general theory behind the flow, see OAuth explained.

Create the OAuth app

Registration happens in the GitHub web interface:

  1. Click your profile picture in the upper-right corner, then click Settings.
  2. In the left sidebar, click Developer settings, then OAuth apps.
  3. Click New OAuth App. If it is your first app, the button reads "Register a new application".
  4. Fill in Application name. Use only information you consider public, because users see it on the authorization screen.
  5. Enter the full URL of your app's website in Homepage URL. A description is optional.
  6. Enter Authorization callback URL: the address on your app that GitHub sends users back to. Click Add callback URL if you need more than one.
  7. Tick Enable Device Flow only if your app uses the device flow, which is meant for tools without a browser, such as command-line programs.
  8. Click Register application.

The Expire user access tokens option is on by default. Leave it on unless your app cannot yet handle short-lived tokens and refresh tokens. After registration, the app's page shows its client ID, and it is where you manage the client secret. Treat the secret like a password: keep it in an environment variable or secrets store on your server, never in a repository or in browser code.

Limits to know about

  • A user or organization can own up to 100 OAuth apps.
  • Each app can have up to 10 callback URLs.
  • You can register under your personal account or any organization where you have admin access. For a team project, an organization-owned app avoids tying the integration to one person's account.

Step 1: send the user to GitHub

Your site redirects the user's browser to:

GET https://github.com/login/oauth/authorize

with these query parameters:

ParameterMeaning
client_idRequired. The client ID GitHub issued for your app.
redirect_uriStrongly recommended. Where GitHub sends the user afterwards. If you omit it, GitHub uses the first callback URL configured for the app.
scopeSpace-delimited list of scopes. If omitted, the user's previously authorized scopes are reused.
stateStrongly recommended. An unguessable random string that protects against cross-site request forgery.
loginOptional. Suggests which account to sign in with.
allow_signupOptional. Defaults to true; set false if your policy prohibits sign-ups during OAuth.
code_challenge, code_challenge_methodStrongly recommended for PKCE. The method must be S256; plain is not supported.

An example URL, split across lines for readability (the real one is a single line):

https://github.com/login/oauth/authorize
  ?client_id=YOUR_CLIENT_ID
  &redirect_uri=https%3A%2F%2Fexample.com%2Fcallback
  &scope=read%3Auser%20user%3Aemail
  &state=3f9a1c7e5b2d4a60

Generate state fresh for each attempt with a cryptographically secure random generator, and store it in the user's session so you can compare it later.

Step 2: handle the callback and swap the code for a token

If the user approves, GitHub redirects back to your callback URL with a code and the state you sent. First compare the returned state with the stored one. If they differ, stop: GitHub's documentation warns that a third party may have created the request. The code expires after 10 minutes.

Then, from your server, exchange the code:

POST https://github.com/login/oauth/access_token

The body parameters are client_id, client_secret, code, and (recommended) the same redirect_uri you used in step 1. If you started with a PKCE code_challenge, you must also send the original code_verifier. A curl sketch, with placeholder values:

curl -X POST https://github.com/login/oauth/access_token \
  -H "Accept: application/json" \
  --data-urlencode "client_id=YOUR_CLIENT_ID" \
  --data-urlencode "client_secret=$GITHUB_CLIENT_SECRET" \
  --data-urlencode "code=CODE_FROM_CALLBACK" \
  --data-urlencode "redirect_uri=https://example.com/callback"

By default the response is URL-encoded form data with access_token, scope and token_type=bearer. Sending Accept: application/json, as above, returns JSON instead, which is easier to parse. Example response shape (values are made up):

{
  "access_token": "gho_exampletoken",
  "scope": "read:user,user:email",
  "token_type": "bearer"
}

If your app has expiring tokens turned on, or you requested offline_access, the response also contains refresh_token, expires_in and refresh_token_expires_in, all in seconds. GitHub's documentation says the access token expires after eight hours and the refresh token after six months without use, so plan to refresh rather than store a token forever.

Step 3: call the GitHub API

Send the token in an Authorization header:

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.github.com/user

For a sign-in feature, this request is the point: it returns the profile of the user who just authorized. GitHub advises revalidating the user's identity every time you receive a token, because the account signed in during authorization can change. Do not assume the person is the same one as in an earlier session.

Pick scopes carefully

Ask for the smallest set of scopes that makes your feature work. Common ones:

  • read:user: read-only access to the user's profile data.
  • user:email: read-only access to the user's email addresses.
  • public_repo: access limited to public repositories, including starring them.
  • repo: full access to public and private repositories, including code, commit statuses, invitations, collaborators, deployment statuses and webhooks.
  • gist: write access to gists.
  • workflow: lets the app add and update GitHub Actions workflow files.

To see what a token can do, make any API request with it and read the response headers. X-OAuth-Scopes lists the scopes the token was authorized for, and X-Accepted-OAuth-Scopes lists the scopes the endpoint checks for. If a request is refused, comparing those two headers usually shows which scope is missing.

Redirect URL rules that cause errors

By default the redirect_uri must exactly match a registered callback URL, so a trailing slash or a different port will fail. GitHub also offers optional wildcard matching, where the host (excluding subdomains) and port must match exactly and the path must be a subdirectory of the callback URL. GitHub says this carries security risks and should be used only if you control every possible subdomain and path, and that apps with a single callback URL created before August 3, 2026 have it enabled by default. If you do not need it, disable it in the app's settings. For native apps, a loopback address such as http://127.0.0.1:1234/path is accepted even when the registered callback uses a different port. GitHub's documentation points to the RFC advice of preferring 127.0.0.1 or ::1 over localhost.

Also note that CORS preflight (OPTIONS) requests are not supported on these endpoints, so run the token exchange server-side rather than from browser JavaScript.

OAuth app or GitHub App?

GitHub states that "in general, GitHub Apps are preferred over OAuth apps". The differences it lists:

  • Permissions: GitHub Apps use granular permissions; OAuth apps use broad scopes such as repo.
  • Repository access: a GitHub App only reaches the repositories chosen at installation, while an OAuth app can reach everything the authorizing user can.
  • Identity: a GitHub App can act as its own bot and keep working without a user present; an OAuth app acts as the user who authorized it, and loses access when that user does.
  • Rate limits: an OAuth app shares the authorizing user's limit of 5,000 requests per hour.

An OAuth app is the more direct fit when you only want users to sign in or to act strictly as themselves. If you need access to selected repositories, automation without a user, or finer permissions, look at a GitHub App. If you have built a similar sign-in for Google, the steps in how to create a Google OAuth client ID are close in shape, and API key vs OAuth explains when a token like this is the right credential.

Security checklist

  • Keep the client secret on the server and out of version control. If it leaks, replace it in the app's settings.
  • Always send and verify state. Add PKCE as well.
  • Request only the scopes you need, and explain why on your own page before redirecting.
  • Exchange the code within 10 minutes and use it once.
  • Store access and refresh tokens as carefully as passwords, and never log them.

Frequently asked questions

Where do I create a GitHub OAuth app?
Click your profile picture, then Settings, then Developer settings, then OAuth apps, then New OAuth App. You can register it under your personal account or under any organization where you have admin access.
How many OAuth apps and callback URLs can I have?
According to GitHub's documentation, a user or organization can own up to 100 OAuth apps, and each app can have up to 10 callback URLs.
Should I use a GitHub App instead of an OAuth app?
GitHub's documentation says GitHub Apps are generally preferred, because they have granular permissions and can be limited to chosen repositories. An OAuth app is still the simpler choice for a plain 'Sign in with GitHub' button, and GitHub says it is preferred when you need enterprise-level resources.
How long does the authorization code last?
The code GitHub sends to your callback URL expires after 10 minutes, so exchange it for an access token immediately.
Is it safe to put the client secret in front-end code?
No. The client secret is only needed for the token exchange, which must happen on a server you control. Anything shipped to a browser can be read by every visitor.

Sources