GitHub API:获取组织所有成员的电子邮件地址

aun*_*uny 9 api github github-api

GitHub API提供了一种获取组织成员的方法.我是我的组织的管理员/所有者,我想获得所有用户的电子邮件地址.我尝试使用membersAPI但它没有返回电子邮件地址.

在我的管理员中使用curl auth-token:

GET /orgs/my-org/members
Run Code Online (Sandbox Code Playgroud)

我得到的响应看起来像这样(对于其中一个用户)

"login": "auny",
    "id": some-number,
    "avatar_url": "https://secure.gravatar.com/avatar/6579d09c459f1adad8a60463f47eadd3?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png",
    "gravatar_id": "6579d09c459f1adad8a60463f47eadd3",
    "url": "https://api.github.com/users/auny",
    "html_url": "https://github.com/auny",
    "followers_url": "https://api.github.com/users/auny/followers",
    "following_url": "https://api.github.com/users/auny/following{/other_user}",
    "gists_url": "https://api.github.com/users/auny/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/auny/starred{/owner} 
     {/repo}",
    "subscriptions_url": "https://api.github.com/users/auny/subscriptions",
    "organizations_url": "https://api.github.com/users/auny/orgs",
    "repos_url": "https://api.github.com/users/auny/repos",
    "events_url": "https://api.github.com/users/auny/events{/privacy}",
    "received_events_url": 
    "https://api.github.com/users/auny/received_events",
    "type": "User"
Run Code Online (Sandbox Code Playgroud)

奇怪的是,这包含有关用户的大量信息,但不包含重要信息之一,即用户的电子邮件地址.作为admin我应该能够获得所有用户的电子邮件地址.

其他用户API仅返回我自己的电子邮件地址,即auth-token请求中使用的帐户的电子邮件地址.API用法:

GET /user/emails
Run Code Online (Sandbox Code Playgroud)

怎么能实现这一目标?周围有路吗?

Ber*_*tel 6

使用GraphQL API v4,您不必为每个用户调用一个请求。以下将获得组织成员及其email(如果在公开资料中提供):

{
  organization(login: "my-org") {
    membersWithRole(first: 100) {
      totalCount
      edges {
        node {
          login
          name
          email
        }
      }
      pageInfo {
        endCursor
        hasNextPage
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在资源管理器中尝试

[编辑 08/2020]使用membersWithRole代替不推荐使用的members字段

  • 组织成员现已成为已弃用的字段。相反,请使用memberStatuses。 (2认同)
  • 对于 2023 年阅读的读者来说,“电子邮件”字段仅提供公共电子邮件,而该电子邮件大部分时间为空。 (2认同)

Tho*_*aux 5

您可以改用git仓库!里面有很多有趣的信息。

尝试运行:

git shortlog -s -n -e
Run Code Online (Sandbox Code Playgroud)

您将看到带有电子邮件地址的提交者列表。

  • 这不足以将电子邮件与 GitHub 用户名关联起来。对于尚未提交到特定存储库的组织成员来说,这也不够。 (3认同)

Iva*_*zak 2

获取组织成员列表后,使用此 API 端点获取每个成员的信息:

/users/:user
Run Code Online (Sandbox Code Playgroud)

这将返回您在请求 URL 中指定的用户的电子邮件地址。

  • 此端点仅为某些用户返回“email”字段,并且该字段也设置为“null”。然而,对于大多数用户来说,“电子邮件”字段甚至不存在。这是否与用户如何配置其帐户有关? (5认同)
  • 我每次都会选择 1。但事实上,它可能不会为我组织中的某个人返回电子邮件,这一事实是荒谬的。Github 不允许公司执行 2FA 已经够糟糕的了,但试图审核和纠缠用户也是不可能的。就像 Github 不希望企业保护其用户的 Github 帐户一样。 (2认同)