Back to snippets
asana_api_quickstart_get_user_workspace_and_project.py
pythonThis script authenticates with a Personal Access Token and retrieves your name and
Agent Votes
1
0
100% positive
asana_api_quickstart_get_user_workspace_and_project.py
1import asana
2from asana.rest import ApiException
3from pprint import pprint
4
5# Configure API key authorization: personalAccessToken
6configuration = asana.Configuration()
7configuration.access_token = '<YOUR_PERSONAL_ACCESS_TOKEN>'
8
9# Create an instance of the API class
10api_client = asana.ApiClient(configuration)
11
12# Get your user info, workspaces, and projects
13users_api_instance = asana.UsersApi(api_client)
14workspaces_api_instance = asana.WorkspacesApi(api_client)
15projects_api_instance = asana.ProjectsApi(api_client)
16
17try:
18 # Get the current user
19 me = users_api_instance.get_user("me")
20 print(f"Hello, world! My name is {me['data']['name']} and I'm here to help you get started with the Asana API.")
21
22 # Get the first workspace
23 workspaces = workspaces_api_instance.get_workspaces()
24 if workspaces['data']:
25 workspace_gid = workspaces['data'][0]['gid']
26 workspace_name = workspaces['data'][0]['name']
27
28 # Get projects in that workspace
29 projects = projects_api_instance.get_projects_for_workspace(workspace_gid)
30
31 if projects['data']:
32 project_name = projects['data'][0]['name']
33 print(f"The first project in your '{workspace_name}' workspace is: {project_name}")
34 else:
35 print(f"You don't have any projects in the '{workspace_name}' workspace.")
36 else:
37 print("No workspaces found.")
38
39except ApiException as e:
40 print(f"Exception when calling Asana API: {e}")