Hi all! I am exploring the Asana API and trying to make my first script. I am having issues figuring out the correct syntax to use for the tasks.add_tag() function. Below is a snippet of one of the many configurations I have tried:
Did you check out the source of the library? It usually helps a lot.
Based on the API doc (Add a tag to a task) I would that that you have the create the tag first, get a gid, and then use this id to add the tag to the task. It is a 2-step process.
Thank you for your response! I did check the source and it could be that the APIs I have used before felt different, but I am still not clear on what the issue with my syntax is.
The task I referenced when running my code already exists and its GID is correct (I was able to pull the tag information when getting the tag via its GID). This tag was created in Asana before running the code.
Is it not possible to use an existing tag, or are you saying that I need to “create it” locally somehow before adding it to a task? When passing the full dict object into the add_tag function it also did not work.
Your code is attaching sample_tag to task gid 1202815185779984 when in fact you should be attaching tag 1234563753663 to task 1202815185779984. Does it make sense?
After the initial creation of the tag, you can’t use its text value anymore, you have to work with its gid.
I apologize! This is one of the things I tried and did not work but not the sample code I put in and I did not remember that. I’m not sure whether the GID needs to go as a params= or data=. I’ll try both in a bit and paste my console results.
So even when passing a tag ID it does not work? I suggest you download their Postman library and run requests from there to make sure the syntax is correct.
It still does not work with a proper id! Here is what the console gives me:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
I will look into downloading their Postman library. I unfortunately have not been able to run Postman in my device before but this is probably worth a couple of hours of troubleshooting.
sample_task = client.tasks.get_task('1203557436241232')
print("Here is the task's name:")
print(sample_task['name'])
sample_tag = client.tags.get_tag('1202490368795511')
print("Here is the tag's color:")
print(sample_tag['color'])
##Code uses the same task and tag
client.tasks.add_tag('1203557436241232','1202490368795511')
And the output:
Here is the task's name:
Sample Task
Here is the tag's color:
dark-green
Traceback (most recent call last):
File "C:\Users\thele\Documents\Development\Asana\Workflow-Automation\workflow.py", line 29, in <module>
client.tasks.add_tag('1203557436241232','1202490368795511')
File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\resources\tasks.py", line 385, in add_tag
return self.client.post(path, params, **options)
File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\client.py", line 197, in post
'data': _merge(parameter_options, data),
File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\client.py", line 367, in _merge
[result.update(obj) for obj in objects]
File "C:\Users\thele\AppData\Local\Programs\Python\Python39\lib\site-packages\asana\client.py", line 367, in <listcomp>
[result.update(obj) for obj in objects]
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Hello. I have the same problem but I use java api library.
When I execute teh following code: client.tasks.addTag("1204525991564977").data("gid", "1204520237142577").execute();
I get the following error:
com.google.api.client.http.HttpResponseException: 400 Bad Request
{"errors":[{"message":"task: Not the correct type","help":"For more information on API status codes and how to handle them, read the docs on errors: https://developers.asana.com/docs/errors"},{"message":"tag: Missing input","help":"For more information on API status codes and how to handle them, read the docs on errors: https://developers.asana.com/docs/errors"}]}
What do I do wrong?
P.S. the tag gid is correct, the tag has been created manually in the UI and it can be added to the task by hand, but I failed to do this via API. Any help is much appriciated.
Can you confirm the first id (1204525991564977) is the task gid and then the other one is the tag? That’s what’s it is supposed to be according to the code specs. Also the tag id should be given as a value for a key called “tag”.