通过主邮箱获取Github用户名

14

我正在使用PyGithub库邀请新成员加入组织。 我遇到的问题是: 在这种情况下,当我只知道用户的主要电子邮件时,如何检索他的用户名以便相应地邀请? 我知道可以通过UI实现,但找不到通过API进行相应调用的方法。 请协助!


不确定完全,但我认为你可以通过电子邮件邀请。 - Arpit Solanki
@ArpitSolanki 是的,他可以,但是它的 API 函数是什么? - Hamza Anis
4个回答

21

使用github 用户搜索API。我尝试了以下方法。

https://api.github.com/search/users?q=solankiarpit1997@gmail.com

关键字 login 在此处表示用户名。

{
  "total_count": 1,
  "incomplete_results": false,
  "items": [
    {
      "login": "arpit1997",
      "id": 10682054,
      "avatar_url": "https://avatars1.githubusercontent.com/u/10682054?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/arpit1997",
      "html_url": "https://github.com/arpit1997",
      "followers_url": "https://api.github.com/users/arpit1997/followers",
      "following_url": "https://api.github.com/users/arpit1997/following{/other_user}",
      "gists_url": "https://api.github.com/users/arpit1997/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/arpit1997/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/arpit1997/subscriptions",
      "organizations_url": "https://api.github.com/users/arpit1997/orgs",
      "repos_url": "https://api.github.com/users/arpit1997/repos",
      "events_url": "https://api.github.com/users/arpit1997/events{/privacy}",
      "received_events_url": "https://api.github.com/users/arpit1997/received_events",
      "type": "User",
      "site_admin": false,
      "score": 52.297474
    }
  ]
}

2
如果用户名为“nateshmbhat1”的用户有一个公开的电子邮件地址“nateshmbhatofficial@gmail.com”,那么只有通过API才能找到他。我看过这个用户的资料,我认为他在资料中将电子邮件地址设置为私人的。 - Arpit Solanki

10

PyGithub API

请参考search_users

search_users(query, sort=NotSet, order=NotSet, **qualifiers)

  • query – 字符串
  • sort – 字符串(‘followers’,‘repositories’,‘joined’)
  • order – 字符串(‘asc’,‘desc’)
  • qualifiers – 关键字字典查询限定词

例如,

g = github.Github("USERNAME", "PASSWORD")
users = g.search_users("franky in:email")
for user in users:
    print(user.login)  # print the selected users' username.

GitHub API

根据 GitHub API 搜索用户,您可以使用关键字 in 指定仅通过公共电子邮件进行搜索。

例如,

https://api.github.com/search/users?q=franky+in:email

然后,您只会得到具有 "franky" 的电子邮件的用户。


0
用户必须设置公共电子邮件地址。主电子邮件地址无法通过REST API进行搜索。

1
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

-1

我在Github上有公开的电子邮件地址,但无法通过该电子邮件地址获取我的用户名。

-bash-4.2$ curl -i -s  https://api.github.com/search/users?q=kamu.raju@gmail.com
    HTTP/1.1 200 OK
    Server: GitHub.com
    Date: Sun, 09 May 2021 15:00:13 GMT
    Content-Type: application/json; charset=utf-8
    Cache-Control: no-cache
    Vary: Accept, Accept-Encoding, Accept, X-Requested-With
    X-GitHub-Media-Type: github.v3; format=json
    Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X- 
    RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X- 
    RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub- 
    Media-Type, Deprecation, Sunset
    Access-Control-Allow-Origin: *
    Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
    X-Frame-Options: deny
    X-Content-Type-Options: nosniff
    X-XSS-Protection: 0
    Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
    Content-Security-Policy: default-src 'none'
    X-RateLimit-Limit: 10
    X-RateLimit-Remaining: 8
    X-RateLimit-Reset: 1620572473
    X-RateLimit-Resource: search
    X-RateLimit-Used: 2
    Accept-Ranges: bytes
    Content-Length: 73
    X-GitHub-Request-Id: 8CAA:5135:2353C0C:3D598CC:6097F908

    {
    "total_count": 0,
    "incomplete_results": false,
    "items": [

     ]
     }

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接