我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

此版本的 GitHub Enterprise 已停止服务 2020-11-12. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

Identifying and authorizing users for GitHub Apps

GitHub 应用程序可以代表用户执行操作,例如创建议题、创建部署和使用其他受支持的端点。

本文内容

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.

Identifying users on your site

To authorize users for standard apps that run in the browser, use the web application flow.

Web 应用程序流程

Using the web application flow, the process to identify users on your site is:

  1. 用户被重定向,以请求他们的 GitHub 身份
  2. 用户被 GitHub 重定向回您的站点
  3. Your GitHub App accesses the API with the user's access token

If you select Request user authorization (OAuth) during installation when creating or modifying your app, step 1 will be completed during app installation. For more information, see "Authorizing users during installation."

1. 请求用户的 GitHub 身份

GET http(s)://[hostname]/login/oauth/authorize

当您的 GitHub 应用程序指定 login 参数后,它将提示拥有特定账户的用户可以用来登录和授权您的应用程序。

参数
名称类型描述
client_id字符串Required. The client ID for your GitHub App. You can find this in your GitHub App settings when you select your app.
redirect_uri字符串用户获得授权后被发送到的应用程序中的 URL。 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字符串This should contain a random string to protect against forgery attacks and could contain any other arbitrary data.
login字符串提供用于登录和授权应用程序的特定账户。

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. 用户被 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.

Note: If you select Request user authorization (OAuth) during installation when creating or modifying your app, GitHub returns a temporary code that you will need to exchange for an access token. The state parameter is not returned when GitHub initiates the OAuth flow during app installation.

Exchange this code for an access token.

POST http(s)://[hostname]/login/oauth/access_token
参数
名称类型描述
client_id字符串Required. The client ID for your GitHub App.
client_secret字符串Required. The client secret for your GitHub App.
代码字符串必填。您收到的响应第 1 步的代码。
redirect_uri字符串用户获得授权后被发送到的应用程序中的 URL。 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字符串您在第 1 步提供的不可猜测的随机字符串。
响应

默认情况下,响应采用以下形式:

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

例如,您可以像以下这样在 curl 中设置“授权”标头:

curl -H "Authorization: token OAUTH-TOKEN" http(s)://[hostname]/api/v3/user

Check which installation's resources a user can access

注:要使用 GitHub 应用程序访问 API,您必须在请求的 Accept 标头中提供自定义媒体类型

application/vnd.github.machine-man-preview+json

警告: 在预览期间,API 可能会更改,恕不另行通知。 预览功能不支持用于生产。 如果遇到任何问题,请联系 your site administrator。

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 app installations accessible to the user access token and List repositories accessible to the user access token.

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. 任何人都可以从 GitHub 帐户设置页面撤销他们对 GitHub 应用程序的授权。 撤销对 GitHub 应用程序的授权不会卸载 GitHub 应用程序。 您应该编程 GitHub 应用程序,使其在收到此 web 挂钩后,不再代表已撤销令牌的人调用 API。 如果 GitHub 应用程序继续使用已撤销的访问令牌,它将收到 401 Bad Credentials 错误。

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

检查运行
检查套件
Codes Of Conduct
Deployment Statuses
部署
事件
馈送
Git Blobs
Git Commits
Git Refs
Git Tags
Git Trees
Gitignore Templates
安装设施
Issue Assignees
Issue Comments
Issue Events
Issue Timeline
议题
标签
许可
Markdown
元数据
里程碑
Organization Hooks
Organization Members
Organization Outside Collaborators
Organization Pre Receive Hooks
Organization Team Repositories
Organization Teams
组织
Project Collaborators
项目
Pull Comments
Pull Request Review Events
Pull Request Review Requests
Pull Request Reviews
拉取
反应
仓库
Repository Activity
Repository Branches
Repository Collaborators
Repository Commit Comments
Repository Commits
Repository Community
Repository Contents
Repository Hooks
Repository Invitations
Repository Keys
Repository Pages
Repository Pre Receive Hooks
Repository Releases
Repository Stats
搜索
状态
Team Discussions
主题
User Emails
User Followers
User Gpg Keys
User Public Keys
用户