Reputation: 5459
Using this link I can see that my (tonylampada) id on github is 218821 https://api.github.com/users/tonylampada
How could I do the opposite? Given the user id = 218821, what's the username?
Update
Answering nulltoken here because it's a long story and it won't fit in a comment.
FreedomSponsors is a django application that uses django-social-auth to enable login with Github (and others). (You should check it out, btw, please see the about page in the blog :-) Django-social-auth has a configuration flag that allows the application to store the github username on the database. A few days ago I deployed a new version of FS with github login enabled, but with "storeGithubUsername" set to false.
A few users registered their github accounts, and now the database has their github ids, but not their usernames.
You can se in my profile that I have github as a "connected account" but there's no link to my github page.
I need it to make the link point to https://github.com/tonylampada
I'm ready to deploy a new version that fixes this, by setting the "storeGithubUsername" (that's not what it is called, I'm just simplifying here) to true. But I'd like to patch the database with the already github-registered users. I have their github ids, but not their github usernames.
Upvotes: 55
Views: 52695
Reputation: 900
There is actually an official, documented endpoint that lets you do this: the endpoint to list users, /users
.
It accepts two parameters you can use to get a specific user ID:
since
- integer, users with IDs greater than this will be returned in orderper_page
- limit number of results per pageSo given a user id USER_ID
, you can make a request with since = USER_ID - 1
and per_page = 1
to get a specific user.
For example, to look up USER_ID = 1000
, you set since=999
like this:
; curl 'https://api.github.com/users?since=999&per_page=1'
[
{
"login": "aslakhellesoy",
"id": 1000,
⋮ snip
"site_admin": false
}
]
Upvotes: 4
Reputation: 652
The answer marked correct here doesn't work anymore. You'll now get
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/users#get-a-user"
}
Now do a Direct Node Lookup via Graphql https://docs.github.com/en/graphql/guides/using-global-node-ids#3-do-a-direct-node-lookup-in-graphql
Upvotes: 4
Reputation: 37
By the time I answer this question, the method that works is: https://api.github.com/user/USER_ID
Remark: It is similar to what Andrew shared in 2015; you just have to remove the colon in the URL he shared.
Upvotes: -2
Reputation: 6205
We need to do this on Gitter to deal with the situation where a user has changed their username on GitHub and we get a 404 response when querying their old username.
Here's an undocumented endpoint, so use as your own peril, but it does work for now.
Use the endpoint: https://api.github.com/user/:id
, where :id
is the ID of the user.
Similar endpoints exist for repos and orgs, at
https://api.github.com/repositories/:id
and https://api.github.com/organizations/:id
respectively.
Note that the new repository redirects preview API only supports repositories, not renamed users or organizations. In fact, the HTTP 301 redirect actually points to https://api.github.com/repositories/:id
, so there's a good chance that these "ID" endpoints may in fact become official soon.
Upvotes: 95
Reputation: 67589
There's no documented feature, nor undocumented ones that I know of, that expose the retrieval of the username from the id. From the GitHub API consumer perspective, the user id is an "implementation detail". The real key is the username.
From what I understand, you only require a batch of usernames given a list of ids. And this would be a one time only request, not a permanent need.
As your request seems legit and limited in its scope, you might get this answer directly from GitHub support by dropping them an email at [email protected].
Indeed, xpaulbettsx, a GitHubber, even tweeted about this:
Support@ is good for Anything you want to tell GitHub - bugs, features, high 5s; everything but security which go to security@
Upvotes: 8