Manage Identity
How to set up Identity to sign in and use the Python SDK.
Read time 3 minutesLast updated 2 months ago
Unity Cloud services require the caller to be properly authorized before you can perform any operation or access other modules such as Assets. There are two modules available to retrieve such authorization:
unity_cloud.identity.user_login
unity_cloud.identity.service_account
unity_cloud.identity.user_login.use()
unity_cloud.identity.service_account
How do I...?
Authenticate with a user login
The user sign-in process requires to:- Manually request a sign-in (or a sign-out) from the integration.
- Enter your Unity account credentials to authenticate.
Initialize and sign in to Unity Dashboard
To use standard user login, follow these steps:- Call to initialize the
unity_cloud.identity.user_login.use()
module. This action automatically checks if any refresh token is currently cached. If so, you are authenticated.user_login
- If there is no token, call to trigger your browser to open and sign in to Unity Dashboard.
login
After you have signed in, a refresh token is automatically cached.
def initialize_user_login(): unity_cloud.initialize() unity_cloud.identity.user_login.use() auth_state = unity_cloud.identity.user_login.get_authentication_state() if auth_state != unity_cloud.identity.user_login.Authentication_State.LOGGED_IN: unity_cloud.identity.user_login.login()
Sign out
To sign out, follow these steps:- Initialize the following modules in this order:
unity_cloud
unity_cloud.identity.user_login.use()
- (Optional) . Read more about initializing and signing in the user through Unity Dashboard.
unity_cloud.identity.user_login.login
- Call the method with the
logout
argument set toclear_cache
.TRUE
Calling the method signs you out from the browser. - (Optional) Set the argument to
clear_cache
to allow automatic re-login next timeFALSE
is called.unity_cloud.identity.user_login.use()
unity_cloud.identity.user_login.logout( clear_cache = True )
Get information about the current user
To retrieve all information on the current user, including their access token, call theget_current_user_info
user_infos = unity_cloud.identity.user_login.get_current_user_info()
Use a callback to track the authentication state during a user sign-in
Subscribing to the authentication state change allows you to get a callback every step of the authentication flow. The states tracked are:AWAITING_INITIALIZATION
AWAITING_LOGIN
LOGGED_IN
AWAITING_LOGOUT
LOGGED_OUT
def auth_state_callback(state): print(state) unity_cloud.identity.user_login.subscribe( event_type = unity_cloud.identity.user_login.AUTHENTICATION_STATE_CHANGED_EVENT, callback = auth_state_callback ) unity_cloud.identity.user_login.login() unity_cloud.identity.user_login.logout(clear_cache = True) unity_cloud.identity.user_login.unsubscribe( event_type = unity_cloud.identity.user_login.AUTHENTICATION_STATE_CHANGED_EVENT, callback = auth_state_callback )
Use a service account to perform app-oriented authorization
The service account allows authorization for internal headless scenarios (automated scripts, tests, etc.).Generate a service account
- Create a service account.
- Make sure to store the service account credentials (and
key ID
) in a secure and private location.secret key
Refer to these credentials in your integration to authorize your calls to Unity Cloud services. - Attach the service account to an organization or a project.
Set up your app to use a service account
After the requiredunity_cloud
key id
secret key
key_id = "1234abcd-ab12-cd34-ef56-123456abcdef" key = "12345678ABCDefgh-1234AbC-5678eFg" unity_cloud.identity.service_account.use(key_id=key_id, key=key)
Get a list of the accessible organizations or projects
To get a list of accessible organizations or projects, follow these steps:- Use the method to retrieve a list of all accessible organizations.
get_organization_list
- Call with an organization id to retrieve the accessible projects contained in it.
get_project_list
- (Optional) Add the parameter to limit the amount of returned results. Leave it untouched or set it to 0 to return everything.
limit_to
- (Optional) Add the parameter to specify the number of collection to skip in the results. Use it in combination with limit_to to page through results.
skip
- (Optional) Add the parameter
Theorgs = unity_cloud.identity.get_organization_list() if len(orgs) != 0: projects = unity_cloud.identity.get_project_list_list(orgs[0].id) permissions = unity_cloud.identity.get_organization_permissions(orgs[0].id) roles = unity_cloud.identity.get_organization_roles(orgs[0].id)
get_organization_permissions
get_organization_roles
get_project_permission
get_project_roles
Create a project
To create a project, follow these steps:- Create a object with the name of the new project and optionally metadata.
ProjectCreation
- Call with the
create_project
object and the organization ID.ProjectCreation
project_creation = ProjectCreation(name="myNewProject") unity_cloud.identity.create_project(project_creation, org_id)
Get a project
To get a single project, callget_project
project = unity_cloud.identity.get_project(org_id, project_id)
List users in an organization
To list all users associated to an organization, callget_organization_user_info_list
users = unity_cloud.identity.get_organization_user_info_list(org_id)
List users in a project
To list all users associated to a project, callget_project_user_info_list
users = unity_cloud.identity.get_project_user_info_list(org_id, project_id)
Get a single user
To get the info for a user, callget_user_info
user = unity_cloud.identity.get_user_info(org_id, user_id)