Deleteman
Deleteman

Reputation: 8690

How to get the list of Organizations a user belongs to in GitHub?

I'm trying to do what the title says, using GitHub's API, I'm trying to get the list of orgs any user belongs to... my problem, is that I can't find a way to do it.

According to the API's doc: https://developer.github.com/v3/orgs/ using this endpoint:

/users/:user/orgs 

That should list them, but it's not doing it for my user, so I'm guessing this only lists orgs created by the user.

If this is the case, is there a way around it somehow, so I can get the list of organizations that any user belongs to?

Upvotes: 8

Views: 10388

Answers (4)

VonC
VonC

Reputation: 1324278

It should but, as mentioned

Since the list orgs API will only list public memberships, regardless of authentication. (more precisely, GET /orgs/{org}/memberships/{username})

If you need to fetch all of the organization memberships (public and private) for the authenticated user, use the List your organizations API instead.

Maybe your user is not part of any public orgs, only private ones.


More recently, using gh, the GitHub CLI after a gh auth login:

gh api \
  -H "Accept: application/vnd.github+json" \
  /user/orgs \
  --jq ".[].login"

One line:

gh api -H "Accept: application/vnd.github+json" /user/orgs --jq ".[].login"

Upvotes: 11

Tiago Peres
Tiago Peres

Reputation: 15451

If you still want to get the user's organizations that your authorization allows you to operate on in some way, you can use the List organizations for the authenticated user endpoint.

In cURL

curl \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <YOUR-TOKEN>"\
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/user/orgs

where you need to substitute the <YOUR-TOKEN> by the access_token you get in the response after authentication.

In my case, using Postman

enter image description here

Upvotes: 0

fkarakas
fkarakas

Reputation: 21

just call /user/orgs to retrieve all organizations public or private

Upvotes: -2

Casiano
Casiano

Reputation: 491

Use the route /user/memberships/orgs. For instance, using GitHub Cli:

$ gh api  /user/memberships/orgs  --jq '.[0].organization.login'
etsii2

Upvotes: 3

Related Questions