0

Trying to show the custom attributes (mainly the location field ) for some public GitLab users. but it's not working

I am using python gitlab library to get the list. and I can print the user names, ids and url. Now I want to access the custom attributes such as the location but it doesn't work with me! I am trying the following from the browser its not working (example: username =gitlab)

https://gitlab.com/api/v4/users?username=gitlab&private_token=mytoken

# this brings the users list
import gitlab
gl = gitlab.Gitlab('http://gitlab.com', private_token='mytoken')
users = gl.users.list()
for u in users:
    print (u.name)
    print (u.id)
    print (u.web_url)
cdboer
  • 95
  • 1
  • 10
Ahmad saleh
  • 103
  • 3

2 Answers2

2

Notice this excerpt from the official API documentation:

Every API call to custom attributes must be authenticated as administrator. Custom attributes are currently available on users, groups, and projects, which will be referred to as “resource” in this documentation.

It seems that the account you use to authenticate yourself with, has to have administrative rights for the queried GitLab Instance. Check whether or not that is the case for your specific account aswell as your GitLab Instance.

You can find the python-gitlab API documentation for custom user attributes in the custom attributes section. Else there is also the official API documentation for this feature.

For your example, a valid call would look like this:

import gitlab

gl = gitlab.Gitlab('http://gitlab.com', private_token='mytoken')
users = gl.users.list()

for user in users:
    attrs = user.customattributes.get('location')
cdboer
  • 95
  • 1
  • 10
  • it didnt work, I got 403 Forbidden error when I use user.customattributes.get('location') – Ahmad saleh Dec 12 '19 at 14:21
  • I also tried the following in the browser: https://gitlab.com/api/v4/users?custom_attributes[location]=usa >> then I got a random user id and tried to get the lcoation for that user knowing that this user has custom attribute but I end up with the same error – Ahmad saleh Dec 12 '19 at 14:47
  • If you try to query the public API then you will likely get a 403. This indicates that you do not have the necessary permissions to perform that action. As noted above, your account needs to have administrative rights to access the custom attributes of users. It seems, that it is not possible to do what you want to do without having admin rights. – cdboer Dec 12 '19 at 16:40
0

This feature is only for admin in GitLab.com

Ron Varghese
  • 136
  • 1
  • 4