PyGitHub:获取用户的私人电子邮件地址和简历

问题描述 投票:0回答:1

我正在尝试使用 PyGitHub 获取与用户相关的所有信息。

但是,当我尝试获取用户的个人简介和电子邮件地址时,它向大多数用户返回“无”。

我认为该用户已使用 GitHub 上的以下设置标记为保持私密:

有什么方法可以使用 PyGitHub 或 REST API 获取此电子邮件地址。

以下是我的代码:

from github import Github
# using username and password
git = Github("access_token")

org = git.get_organization('org')
for m in org.get_members():
     i = i+1
     print(i)
     email_address = str(m.email)
     print(type(email_address))
     print('Bio:',m.bio)
python github-api pygithub
1个回答
0
投票

在python中,使用PyGithub,可以访问用户的私人电子邮件(前提是获得的访问令牌已经在范围内定义了“user:emails”)

from github import Github
github_api = Github("access_token")
github_user = github_api.get_user()
emails = github_user.get_emails()

primary_email = None
for email_inst in emails:
    if email_inst.primary:
        primary_email = email_inst.email

这是来自 next-js GitHub Provider 的另一个示例:

        const profile = await client.userinfo(tokens.access_token!)

        if (!profile.email) {
          // If the user does not have a public email, get another via the GitHub API
          // See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user
          const res = await fetch("https://api.github.com/user/emails", {
            headers: { Authorization: `token ${tokens.access_token}` },
          })

          if (res.ok) {
            const emails: GithubEmail[] = await res.json()
            profile.email = (emails.find((e) => e.primary) ?? emails[0]).email
          }
        }

        return profile
© www.soinside.com 2019 - 2024. All rights reserved.