Identifying and authorizing users for GitHub Apps
Your GitHub App can perform actions on behalf of a user, like creating an issue, creating a deployment, and using other supported endpoints.
- Identifying users on your site
- Check which installation's resources a user can access
- Handling a revoked GitHub App authorization
- User-level permissions
- User-to-server requests
When your GitHub App acts on behalf of a user, it performs user-to-server requests. These requests must be authorized with a user's access token. User-to-server requests include requesting data for a user, like determining which repositories to display to a particular user. These requests also include actions triggered by a user, like running a build.
Note: To access the API with your GitHub App, you must provide a custom media type in the Accept Header for your requests.
application/vnd.github.machine-man-preview+json
Warning: The API may change without advance notice during the preview period. Preview features are not supported for production use. If you experience any issues, contact GitHub Enterprise Support.
Identifying users on your site
The flow to identify users on your site is:
- Users are redirected to request their GitHub identity
- Users are redirected back to your site by GitHub
- Your GitHub App accesses the API with the user's access token
1. Request a user's GitHub identity
GET http(s)://[hostname]/login/oauth/authorize
When your GitHub App specifies a login parameter, it prompts users with a specific account they can use for signing in and authorizing your app.
Parameters
| Name | Type | Description |
|---|---|---|
client_id |
string |
Required. The OAuth client ID for your GitHub App. You can find this in your GitHub App settings when you select your app. |
redirect_uri |
string |
The URL in your application where users will be sent after authorization. This must be an exact match to the URL you provided in the User authorization callback URL field when setting up your GitHub App and can't contain any additional parameters. |
state |
string |
This should contain a random string to protect against forgery attacks and could contain any other arbitrary data. |
login |
string |
Suggests a specific account to use for signing in and authorizing the app. |
Note: You don't need to provide scopes in your authorization request. Unlike traditional OAuth, the authorization token is limited to the permissions associated with your GitHub App and those of the user.
2. Users are redirected back to your site by GitHub
If the user accepts your request, GitHub redirects back to your site with a temporary code in a code parameter as well as the state you provided in the previous step in a state parameter. If the states don't match, the request was created by a third party and the process should be aborted.
Exchange this code for an access token:
POST http(s)://[hostname]/login/oauth/access_token
Parameters
| Name | Type | Description |
|---|---|---|
client_id |
string |
Required. The OAuth client ID for your GitHub App. |
client_secret |
string |
Required. The OAuth client secret for your GitHub App. |
code |
string |
Required. The code you received as a response to Step 1. |
redirect_uri |
string |
The URL in your application where users are sent after authorization. |
state |
string |
The unguessable random string you provided in Step 1. |
Response
By default, the response takes the following form:
access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer
3. Your GitHub App accesses the API with the user's access token
The user's access token allows the GitHub App to make requests to the API on behalf of a user.
Authorization: token OAUTH-TOKEN
GET http(s)://[hostname]/api/v3/user
For example, in curl you can set the Authorization header like this:
curl -H "Authorization: token OAUTH-TOKEN" http(s)://[hostname]/api/v3/user
Check which installation's resources a user can access
Once you have an OAuth token for a user, you can check which installations that user can access.
Authorization: token OAUTH-TOKEN
GET /user/installations
You can also check which repositories are accessible to a user for an installation.
Authorization: token OAUTH-TOKEN
GET /user/installations/:installation_id/repositories
More details can be found in: List installations for a user and List repositories accessible to the user for an installation.
Handling a revoked GitHub App authorization
If a user revokes their authorization of a GitHub App, the app will receive the github_app_authorization webhook by default. GitHub Apps cannot unsubscribe from this event. Anyone can revoke their authorization of a GitHub App from their GitHub account settings page. Revoking the authorization of a GitHub App does not uninstall the GitHub App. You should program your GitHub App so that when it receives this webhook, it stops calling the API on behalf of the person who revoked the token. If your GitHub App continues to use a revoked access token, it will receive the 401 Bad Credentials error.
User-level permissions
You can add user-level permissions to your GitHub App to access user resources, such as user emails, that are granted by individual users as part of the user authorization flow. User-level permissions differ from repository and organization-level permissions, which are granted at the time of installation on an organization or user account.
You can select user-level permissions from within your GitHub App's settings in the User permissions section of the Permissions & webhooks page. For more information on selecting permissions, see "Editing a GitHub App's permissions."
When a user installs your app on their account, the installation prompt will list the user-level permissions your app is requesting and explain that the app can ask individual users for these permissions.
Because user-level permissions are granted on an individual user basis, you can add them to your existing app without prompting users to upgrade. You will, however, need to send existing users through the user authorization flow to authorize the new permission and get a new user-to-server token for these requests.
User-to-server requests
While most of your API interaction should occur using your server-to-server installation access tokens, certain endpoints allow you to perform actions via the API using a user access token. Your app can make the following requests using GraphQL v4 or REST v3 endpoints.
Supported endpoints
Check Runs
- Create a check run
- Get a single check run
- Update a check run
- List annotations for a check run
- List check runs in a check suite
- List check runs for a specific ref
Check Suites
Codes Of Conduct
Deployment Statuses
Deployments
Feeds
Git Blobs
Git Commits
Git Refs
Git Tags
Git Trees
Gitignore Templates
Installations
Issue Assignees
Issue Comments
- List comments on an issue
- Create a comment
- List comments in a repository
- Get a single comment
- Edit a comment
- Delete a comment
Issue Events
Issue Timeline
Issues
- List issues
- List assignees
- Check assignee
- List issues for a repository
- Create an issue
- Get a single issue
- Edit an issue
- Lock an issue
- Unlock an issue
Labels
- List labels on an issue
- Add labels to an issue
- Replace all labels for an issue
- Remove all labels from an issue
- Remove a label from an issue
- List all labels for this repository
- Create a label
- Get a single label
- Update a label
- Delete a label
- Get labels for every issue in a milestone
Licenses
Markdown
Meta
Milestones
- List milestones for a repository
- Create a milestone
- Get a single milestone
- Update a milestone
- Delete a milestone
Organization Hooks
Organization Members
- Members list
- Check membership
- Remove a member
- Get organization membership
- Add or update organization membership
- Remove organization membership
- Public members list
- Check public membership
- Publicize a user's membership
- Conceal a user's membership
Organization Outside Collaborators
Organization Pre Receive Hooks
- List pre-receive hooks
- Get a single pre-receive hook
- Update pre-receive hook enforcement
- Remove enforcement overrides for a pre-receive hook
Organization Team Projects
Organization Team Repositories
- List team repos
- Check if a team manages a repository
- Add or update team repository
- Remove team repository
Organization Teams
- List teams
- Edit team
- Get team membership
- Add or update team membership
- Remove team membership
- List child teams
- List user teams
Organizations
- List all organizations
- Get your organization membership
- Edit your organization membership
- List your organizations
- List user organizations
Project Collaborators
- List collaborators
- Add user as a collaborator
- Remove user as a collaborator
- Review a user's permission level
Projects
- List organization projects
- Create an organization project
- Get a project
- Update a project
- Delete a project
- List project columns
- Create a project column
- Get a project column
- Update a project column
- Delete a project column
- List project cards
- Create a project card
- Move a project column
- Get a project card
- Update a project card
- Delete a project card
- Move a project card
- List repository projects
- Create a repository project
Pull Comments
- List comments on a pull request
- Create a comment
- List comments in a repository
- Get a single comment
- Edit a comment
- Delete a comment
Pull Request Review Events
Pull Request Review Requests
Pull Request Reviews
Pulls
- List pull requests
- Create a pull request
- Get a single pull request
- Update a pull request
- List commits on a pull request
- List pull requests files
- Get if a pull request has been merged
- Merge a pull request (Merge Button)
Reactions
- Delete a reaction
- List reactions for a commit comment
- Create reaction for a commit comment
- List reactions for an issue
- Create reaction for an issue
- List reactions for an issue comment
- Create reaction for an issue comment
- List reactions for a pull request review comment
- Create reaction for a pull request review comment
- List reactions for a team discussion comment
- Create reaction for a team discussion comment
- List reactions for a team discussion
- Create reaction for a team discussion
Repositories
- List organization repositories
- Create
- Get
- Edit
- Delete a repository
- Compare two commits
- List contributors
- List forks
- Create a fork
- List languages
- List tags
- List your repositories
- List user repositories
Repository Activity
Repository Branches
- List branches
- Get branch
- Get branch protection
- Update branch protection
- Remove branch protection
- Get admin enforcement of protected branch
- Add admin enforcement of protected branch
- Remove admin enforcement of protected branch
- Get pull request review enforcement of protected branch
- Update pull request review enforcement of protected branch
- Remove pull request review enforcement of protected branch
- Get required signatures of protected branch
- Add required signatures of protected branch
- Remove required signatures of protected branch
- Get required status checks of protected branch
- Update required status checks of protected branch
- Remove required status checks of protected branch
- List required status checks contexts of protected branch
- Add required status checks contexts of protected branch
- Replace required status checks contexts of protected branch
- Remove required status checks contexts of protected branch
- Get restrictions of protected branch
- Remove restrictions of protected branch
- List team restrictions of protected branch
- Add team restrictions of protected branch
- Replace team restrictions of protected branch
- Remove team restrictions of protected branch
- List user restrictions of protected branch
- Add user restrictions of protected branch
- Replace user restrictions of protected branch
- Remove user restrictions of protected branch
- Perform a merge
Repository Collaborators
- List collaborators
- Check if a user is a collaborator
- Add user as a collaborator
- Remove user as a collaborator
- Review a user's permission level
Repository Commit Comments
- List commit comments for a repository
- Get a single commit comment
- Update a commit comment
- Delete a commit comment
- List comments for a single commit
- Create a commit comment
Repository Commits
Repository Community
Repository Contents
- Get archive link
- Get contents
- Create or update a file
- Delete a file
- Get the contents of a repository's license
- Get the readme
Repository Hooks
Repository Invitations
- List invitations for a repository
- Update a repository invitation
- Delete a repository invitation
- List a user's repository invitations
- Accept a repository invitation
- Decline a repository invitation
Repository Keys
Repository Pages
Repository Pre Receive Hooks
- List pre-receive hooks
- Get a single pre-receive hook
- Update pre-receive hook enforcement
- Remove enforcement overrides for a pre-receive hook
Repository Releases
- List releases for a repository
- Create a release
- Get a single release
- Edit a release
- Delete a release
- List assets for a release
- Get a single release asset
- Edit a release asset
- Delete a release asset
- Get the latest release
- Get a release by tag name
Repository Stats
- Get the number of additions and deletions per week
- Get the last year of commit activity data
- Get contributors list with additions, deletions, and commit counts
- Get the weekly commit count for the repository owner and everyone else
- Get the number of commits per hour in each day
Root
Search
Statuses
Team Discussions
- List discussions
- Create a discussion
- Get a single discussion
- Edit a discussion
- Delete a discussion
- List comments
- Create a comment
- Get a single comment
- Edit a comment
- Delete a comment
Topics
User Emails
- List email addresses for a user
- Add email address(es)
- Delete email address(es)
- List public email addresses for a user
User Followers
- List followers of a user
- List users followed by another user
- Check if you are following a user
- Follow a user
- Unfollow a user
- Check if one user follows another
User Gpg Keys
User Public Keys
- List your public keys
- Create a public key
- Get a single public key
- Delete a public key
- List public keys for a user