Search code examples
pythonpython-2.7urllib2urllibtrello

Add Member to Card with Trello API and Python


I am having a hard time using my python script to add a Member to a card. I am looping through a list of tuples with the Card IDs I'm trying to update. I can execute a PUT command just fine but executing a POST command is resulting with an an Error. I'm relatively new to Python scripting and am unsure how to troubleshoot any further to figure out what I'm doing wrong....please help.

Executing the PUT statement isn't working for me because it removes any existing members from the Card and just adds the member I specify.....the ideal situation is to just use the POST command to add the member to the card.

Here is the code I'm using:

for card in cards: 
    try:
        post_url = 'https://api.trello.com/1/cards/' + card[0] + '?key=<myKey>&token=<myWriteToken>'
        opener = urllib2.build_opener(urllib2.HTTPHandler)
        request = urllib2.Request(post_url, data='idMembers=<myMemberID>')
        request.get_method = lambda: 'POST'
        url = opener.open(request)
    except urllib2.HTTPError, e: print 'ERROR occured'
        print e.code
        print e.read()

I have also tried it this way:

for card in cards: 
    try:
        post_url = 'https://api.trello.com/1/cards/' + card[0] + '?key=<myKey>&token=<myWriteToken>'
        opener = urllib2.build_opener(urllib2.HTTPHandler)
        query_args = {'idMembers':'<myMemberID>'}
        encoded_args = urllib.urlencode(query_args)
        request = urllib2.Request(post_url, encoded_args)
        request.get_method = lambda: 'POST'
        url = opener.open(request)
    except urllib2.HTTPError, e: print 'ERROR occured'
        print e.code
        print e.read()

Solution

  • You don't wanna POST to /1/cards/(idCard) -- that isn't even a valid route. To add a member to a card, you wanna POST to /1/cards/(idCard)/idMembers.