Skip to content

Instantly share code, notes, and snippets.

@thedch
Last active December 21, 2021 02:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thedch/4ad472fc615e3b67716bbb4305fc65d9 to your computer and use it in GitHub Desktop.
Save thedch/4ad472fc615e3b67716bbb4305fc65d9 to your computer and use it in GitHub Desktop.
Helper functions to create a task in a specific section in Asana
# The current Asana Python client does not seem to support task creation in a given section.
# The API documentation is also somewhat lacking, and there's some conflicting instructions on various forums.
# After a bit of searching + trial and error, I figured out how to do it using the requests library.
# I'm posting these helper functions here for anyone who may need them in the future.
import requests
import json
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer_token_here', # Add bearer token here!
}
base_url = 'https://app.asana.com/api/1.0/'
# Handy debugging helper function
def printResponse(resp):
print(json.dumps(resp.json(), indent=4, sort_keys=True))
# Gets the default workspace for the account
def getWorkspace():
r = requests.get(base_url + 'users/me', headers=headers)
myWorkspaceID = r.json()['data']['workspaces'][0]['id']
return myWorkspaceID
# Creates a new project in a given workspace
def createProject(name, workspaceID):
data = {'data': {'name': name, 'workspace': workspaceID, 'layout': 'board'}}
r = requests.post(base_url + 'projects', headers=headers, json=data)
newProjectID = r.json()['data']['id']
return newProjectID
# Creates a new section in a given project
def createSection(name, projectID):
data = {'data': {'name': name}}
r = requests.post(base_url + 'projects/' + str(projectID) + '/sections', headers=headers, json=data)
newSectionID = r.json()['data']['id']
return newSectionID
# Creates a new task in a given section, inside a project, inside a workspace
def createTask(name, workspaceID, projectID, sectionID):
memberships = [{'project': projectID, 'section': sectionID}]
data = {'data': {'name': name, 'workspace': workspaceID, 'memberships': memberships}}
r = requests.post(base_url + 'tasks', headers=headers, json=data)
if r.status_code != 201:
s = 'Creation of task with name ' + name + ' failed:'
raise ValueError(s, r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment