AYON Python api to get project names

Hi,
I have recently started using ayon and I am trying to use it’s python api aython-python-api to retrieve information about projects. While retrieving the information I am getting a 401 Client Error as: ayon_api.exceptions.HTTPRequestError: 401 Client Error: Unauthorized for url

I have passed the base_url and token while creating an instance of the ServerAPI class from the python module.

I have attached the code snippet along with this topic for reference. I have replaced the url and token with some text in it.

get_projects_snippet

I would really appreciate if anyone could help me out with this. Also it would be really helpful if anyone can point me to some resources regarding the documentation of the python api for ayon.

Are you making this request from your code editor or from the ayon launcher (openpype) console ?
I think it shouldn’t make a difference,
but, I’m just curious.

I am making this request from a code editor.

Are you sure the token is valid? From where did you get it?

con = ServerApi("http://localhost:5000", token="...rand0m-t0ken...")
print(con.has_valid_token)
# >>> True

# How to possibly receive valid token if it print out 'False'
con.login("<username>", "<password>")
print(con.access_token)
1 Like

Thanks a lot for your for the help. I was getting False while checking for valid token so I generated a new token using the method that you mentioned above and it is working properly. Also I would like to ask that does a token generated in this manner gets expired after a certain time period or can we use this generated token with multiple other instances of ServerAPI class later on.
Also is there a way to generate a token from the ayon’s web app so that it can be used in multiple applications that are using the ayon-python-api. (For example that way in which we create Scripts thourgh Shotgrid web app and use that script name and token in other python based applications where we are using the shotgrid’s python api.)

Also I would like to ask that does a token generated in this manner gets expired after a certain time period or can we use this generated token with multiple other instances of ServerAPI class later on.

You can use the same for multiple instances of ServerAPI. It will expire if is not used for a day (I think it is a day?).

Also is there a way to generate a token from the ayon’s web app so that it can be used in multiple applications that are using the ayon-python-api.

That is possible only for “service user”. Service user can have generated api key in web ui.

I tried creating an api key from the web ui for the service user but when I use that key as token within the ServerAPI I am getting the same error as I was getting in the first place which is:
ayon_api.exceptions.HTTPRequestError: 401 Client Error: Unauthorized for url

I created the api key by going at Settings → Users → (Selected the service user) → Generated the api key

But when I am generating the api key using the login() method, I am getting the results without any error

It does work on my side without any issues…

Which version of server are you using?

  • You can find it out at the bottom (next to Restart Server button) when you click
    image

Stupid question: Are you sure you’ve copied the key right?

I am using the server version 0.3.1
ayon_server_version

I have copied the key correctly that is generated for the service user that I created

I forgot about one part. To create a session. It could be created automatically if token is available on init → will make PR.

con = ServerApi("http://localhost:5000", token="...rand0m-t0ken...")
con.create_session()

EDITED:
PR Create session on ServerAPI initialization by iLLiCiTiT · Pull Request #69 · ynput/ayon-python-api · GitHub

1 Like

I tried creating a session and it is working perfectly fine. Thanks a lot for the response. Also if we close the session using con.close_session() , after closing the session can we call other methods using con, should it give an error since the session is closed or should it give the correct result
Example:
from ayon_api.server_api import ServerAPI
con = ServerAPI(base_url="http://localhost:5000", token="token_goes_here")
con.create_session()
product_types = con.get_project_product_types(project_name="demo_Big_Feature")
print(projects)
con.close_session()
folders = connection.get_folder_by_name(project_name="demo_Big_Episodic", folder_name="assets")
print(folders)

Result:
['demo_Big_Episodic', 'demo_Big_Feature', 'demo_Commercial', 'ResearchAndDevelopment']
{'label': None, 'attrib': {'fps': 25.0, 'pixelAspect': 1.0, 'handleStart': 0, 'resolutionHeight': 1080, 'clipOut': 1 ...

So was it suppose to print the result for folders or was it suppose to give any error as the session was closed before that

The ServerAPI does not need session to call server endpoints. Session helps to keep connection with server, if session is not created a new connection is created on each call. Only requirement to create ServerAPI object is url. There are some endpoints that don’t require authentication.

For what it’s worth…
I was searching for something about AYON Python API on forums and found myself in this post.
So, if someone here is looking for some code snippet to get project names, here it is:

# This works in ayon launcher console
from ayon_core.client import get_ayon_server_api_connection

con = get_ayon_server_api_connection()
projects = con.get_projects()
projects_names = list(map(lambda b: b["name"], projects))
print("Current Projects:", projects_names)
# This works in any where as long as you have ayon-python-api
# pip install ayon-python-api
import ayon_api

con = ayon_api.get_server_api_connection()
projects = con.get_projects()
projects_names = list(map(lambda b: b["name"], projects))
print("Current Projects:", projects_names)