# 管理企业帐户

您可以使用 GraphQL API 管理企业帐户及其拥有的组织。

## 关于使用 GraphQL 管理企业帐户

为帮助您监测和更改组织以保持合规性，可以使用企业帐户 API 和审核日志 API，这些 API 仅以 GraphQL API 的形式提供。

企业帐户终结点适用于 GitHub Enterprise Cloud 和 GitHub Enterprise Server。

使用 GraphQL 可以请求并仅返回指定的数据。 例如，可以创建 GraphQL 查询或请求信息，以查看添加到组织的所有新组织成员。 或者，您可以进行变更或修改，以便邀请管理员加入您的企业帐户。

使用审核日志 API，您可以监视何时有人：

* 访问您的组织或代码库设置。
* 更改权限。
* 在组织、存储库或团队中添加或删除用户。
* 将用户提升为管理员。
* 更改GitHub应用的权限。

使用审核日志 API 可以保留审核日志数据的副本。 对于使用审核日志 API 执行的查询，GraphQL 响应最多可包含 90 至 120 天的数据。 有关审核日志 API 提供的字段列表，请参阅“[企业管理](/zh/enterprise-server@3.22/graphql/reference/enterprise-admin#interface-auditentry)”。

通过企业帐户 API，可以：

* 列出并审查属于企业帐户的所有组织和仓库。
* 更改企业帐户设置。
* 配置企业帐户及其组织的设置策略。
* 邀请管理员加入您的企业帐户。
* 在企业帐户中创建新组织。

有关企业帐户 API 可用的字段的列表，请参阅 [管理企业帐户](/zh/enterprise-server@3.22/graphql/guides/managing-enterprise-accounts#graphql-fields-and-types-for-the-enterprise-accounts-api)。

## 企业帐户 GraphQL 入门指南

请参阅 [使用 GraphQL 客户端](/zh/enterprise-server@3.22/graphql/guides/using-graphql-clients)，以开始使用 GraphQL 管理企业帐户。

如需一些示例查询，请参阅[使用企业帐户 API 的示例查询](#an-example-query-using-the-enterprise-accounts-api)。

### 1. 使用您的 personal access token 进行身份验证

1. 要通过 GraphQL 进行身份验证，您需要在开发者设置中生成一个 personal access token。 有关详细信息，请参阅 [管理个人访问令牌](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)。

2. 授予你的 personal access token 对你希望访问的企业区域的 admin 和完全控制权限。 若要获得对私有仓库、组织、团队、用户数据以及企业计费和个人资料数据的完整访问权限，我们建议为您的 personal access token 选择以下权限范围：

   * `repo`
   * `admin:org`
   * `user`
   * `admin:enterprise`

   企业帐户特定作用域包括：

   * `admin:enterprise`：全面控制企业（包括 `manage_runners:enterprise`、`manage_billing:enterprise` 和 `read:enterprise`）
   * `manage_billing:enterprise`：读取和写入企业计费数据。
   * `manage_runners:enterprise`：可管理 GitHub Actions 企业级运行器及运行器组的访问权限。
   * `read:enterprise`：读取企业资料数据。

3. 复制你的 personal access token，并在将其添加到 GraphQL 客户端之前，将其妥善保存在安全的地方。

### 2. 选择 GraphQL 客户端

建议您使用 GraphiQL 或可用于配置基准 URL 的其他独立 GraphQL 客户端。

也可以考虑使用以下 GraphQL 客户端：

* [Insomnia](https://support.insomnia.rest/article/176-graphql-queries)
* [GraphiQL](https://www.gatsbyjs.org/docs/running-queries-with-graphiql/)
* [Postman](https://learning.getpostman.com/docs/postman/sending_api_requests/graphql/)

接下来将使用 Insomnia。

### 3. 配置 Insomnia 以使用 GitHub GraphQL API 与企业帐户集成

1. 将基 url 和 `POST` 方法添加至 GraphQL 客户端。 使用 GraphQL 请求信息（查询）、更改信息（突变）或使用 GitHub API 传输数据时，默认 HTTP 方法`POST`，基 URL 遵循以下语法：
   * 针对您的企业实例：`https://<HOST>/api/graphql`
   * 对于 GitHub Enterprise Cloud：`https://api.github.com/graphql`
   * 对于具有数据驻留的 GitHub Enterprise Cloud：`https://api.SUBDOMAIN.ghe.com/graphql`

2. 选择“身份验证”菜单，然后单击“持有者令牌”。 如果之前选择了其他身份验证方法，则菜单将改为使用该方法（例如“基本身份验证”）进行标记。

![Insomnia 中展开的“身份验证”菜单的屏幕截图。 菜单标签“身份验证”和“持有者令牌”选项以深橙色框出。](/assets/images/developer/graphql/insomnia-bearer-token-option.png)

1. 在“TOKEN”字段中，输入你在前一步中获得的 personal access token。

![Insomnia 中“持有者”身份验证设置的屏幕截图。 “令牌”字段以深橙色框出。](/assets/images/developer/graphql/insomnia-base-url-and-pat.png)

1. 单击“标头”。

![Insomnia 中的“设置”选项卡的屏幕截图。 “标头”选项卡以深橙色框出。](/assets/images/developer/graphql/json-content-type-header.png)

1. 在“标头”选项卡下，单击“添加”。
2. 在“标头”字段中，输入 `Content-Type`。
3. 在“值”字段中，输入 `application/json`。

现在可以开始执行查询了。

## 使用企业账户 API 的查询示例

此 GraphQL 查询使用 Enterprise Accounts API 请求每个设备的组织中 `public` 存储库的总数。 要自定义此查询，请用企业帐户的标识替换 `<enterprise-account-name>`。 例如，如果企业帐户位于 `https://github.com/enterprises/octo-enterprise`，请将 `<enterprise-account-name>` 替换为 `octo-enterprise`。

```graphql
query publicRepositoriesByOrganization($slug: String!) {
  enterprise(slug: $slug) {
    ...enterpriseFragment
  }
}

fragment enterpriseFragment on Enterprise {
  ... on Enterprise{
    name
    organizations(first: 100){
      nodes{
        name
        ... on Organization{
          name
          repositories(privacy: PUBLIC){
            totalCount
          }
        }
      }
    }
  }
}

# Passing our Enterprise Account as a variable
variables {
  "slug": "<enterprise-account-name>"
}
```

下一个 GraphQL 查询示例显示了在不使用企业帐户 API 的情况下检索每个组织中的 `public` 存储库总数的难度。 请注意，GraphQL 企业账户 API 已使企业执行此任务变得更简单，因为您只需要自定义单个变量。 要自定义此查询，请将 `<name-of-organization-one>` 和 `<name-of-organization-two>` 等项替换为实例中的组织名称。

```graphql
# Each organization is queried separately
{
  organizationOneAlias: organization(login: "nameOfOrganizationOne") {
    # How to use a fragment
    ...repositories
  }
  organizationTwoAlias: organization(login: "nameOfOrganizationTwo") {
    ...repositories
  }
  # organizationThreeAlias ... and so on up-to lets say 100
}

## How to define a fragment
fragment repositories on Organization {
  name
  repositories(privacy: PUBLIC){
    totalCount
  }
}
```

## 分别查询每个组织

```graphql
query publicRepositoriesByOrganization {
  organizationOneAlias: organization(login: "<name-of-organization-one>") {
    # How to use a fragment
    ...repositories
  }
  organizationTwoAlias: organization(login: "<name-of-organization-two>") {
    ...repositories
  }
  # organizationThreeAlias ... and so on up-to lets say 100
}
# How to define a fragment
fragment repositories on Organization {
  name
  repositories(privacy: PUBLIC){
    totalCount
  }
}
```

此 GraphQL 查询用于请求企业组织的最后 5 个日志条目。 要自定义此查询，请替换 `<org-name>` 和 `<user-name>`。

```graphql
{
  organization(login: "<org-name>") {
    auditLog(last: 5, query: "actor:<user-name>") {
      edges {
        node {
          ... on AuditEntry {
# Get Audit Log Entry by 'Action'
            action
            actorLogin
            createdAt
# User 'Action' was performed on
           user{
              name
                email
            }
          }
        }
      }
    }
  }
}
```

有关开始使用 GraphQL 的详细信息，请参阅 [GraphQL 简介](/zh/enterprise-server@3.22/graphql/guides/introduction-to-graphql) 和 [使用 GraphQL 建立调用](/zh/enterprise-server@3.22/graphql/guides/forming-calls-with-graphql)。

## 企业账户 API 的 GraphQL 字段和类型

要获取有关可与企业帐户 API 一起使用的新查询、变更和架构定义类型的详细信息，请参见任何 [GraphQL 参考页面](/zh/enterprise-server@3.22/graphql)上的边栏，其中包含详细的 GraphQL 定义。

可以通过 GraphQL 客户端访问参考文档。 有关详细信息，请参阅 [使用 GraphQL 客户端](/zh/enterprise-server@3.22/graphql/guides/using-graphql-clients)。
有关身份验证和速率限制详细信息等其他信息，请查看 [guides](/zh/enterprise-server@3.22/graphql/guides)。