2

I'm using the Github3 library to access the Github Enterprise API. I'm trying to get all organizations a specific user has, but after I got the generator of ShortOrganization, I don't know how to actually get the organization name, can anyone help?

Here is my attempt:

ghe = github3.enterprise_login(url=url, token=access_token)
user = ghe.user('myu')
iter_org = user.organizations()
print(iter_org)
org_list = []
for org_name in iter_org:
    org_list.append(org_name.login)
print(org_list)

Below is my current output:

<GitHubIterator [-1, /api/v3/users/myu/orgs]>
 []

Where I did wrong?

girlvsdata
  • 1,596
  • 11
  • 21
Minwu Yu
  • 311
  • 1
  • 6
  • 24

1 Answers1

1

.organizations() call requires an authenticated user. When you do

user = ghe.user('myu')

you are only getting the user. For authenticating the user and then getting all the organizations, I tried the following approach and it is working:

from github3 import login

gh = login('username', password='password')
organizations = gh.organizations()

for org in organizations:
    org = org.refresh()
    print(org.login)
Waleed Iqbal
  • 106
  • 6
  • I actually called the organizations under User object, https://github3.readthedocs.io/en/develop/api-reference/users.html#github3.users.ShortUser.organizations. I authenticated my account and try to find an unauthenticated user's organization list – Minwu Yu Aug 01 '18 at 16:35
  • that is what I tried to explain :) you cannot get unauthenticated user's organization list. In your case, you can only get your organization list. – Waleed Iqbal Aug 01 '18 at 16:54
  • hmm, can you show me a working example by calling the organization function under an authenticated user? I tried: `user = ghe.me() org = user.organizations() for o in org: print(o.login) ` It still print nothing – Minwu Yu Aug 01 '18 at 17:31
  • After a lot of research, I figured that the way in which you call organizations() right now will only list public memberships. If the organization membership is public as explained [here](https://help.github.com/articles/publicizing-or-hiding-organization-membership/), your code will work. For both public and private organizations, you will need to used the code which I wrote above. – Waleed Iqbal Aug 01 '18 at 18:14