avatar casper Jul 26. 2010. 12:55 am
Hi,
I am trying to use the API using Python. I've tried a few things, but can't seem to add any objects (reading seems to work fine). If you have any examples that you could share I'd appreciate it.
Casper
avatar Ilija Studen Staff Jul 26. 2010. 8:54 am
Hello Casper,

Do you have write support enabled in API (it's disabled by default)?
avatar casper Jul 26. 2010. 8:25 pm
Yes, I do...
avatar Ilija Studen Staff Jul 27. 2010. 3:48 am
Hello Casper,

In that case, make sure that you are sending "submitted" POST variable with value set to "submitted". This variable CAN'T be sent through GET - it needs to be send as POST variable and if it's not present, activeCollab will simply ignore your request.
avatar casper Jul 27. 2010. 9:46 pm
Hi Ilija,

I am using the "submitted" POST variable... This is my code:

import urllib
import urllib2

params = urllib.urlencode({'path_info':'/projects/2/milestones',
'token':"xxxxxxxxxxxxxxxxxx",
'format':'xml',
'submitted':'submitted'})

req = urllib2.Request("https://our_ac_site.com.au/public/api.php", params)
print "Request Method :",req.get_method() # confirm that it's a POST...
f = urllib2.urlopen(req)
print f.read()

When I run the code I see that it is indeed a POST and that I get redirected to the login page... Any suggestions?
avatar Ilija Studen Staff Jul 28. 2010. 9:53 am
Hello,

I'm not a Python programmer, but this code does not seam to send "submitted" variable as POST, but as GET parameter... Or it sends all parameters as POST? In that case you are not sending path_info and token properly, so you'll not be properly authenticated. Either way, it will not work.

Here's how parameters should be sent: path_info, token and format as GET, and submitted as POST. Please consult Python documentation to learn how to make such request.
avatar casper Jul 29. 2010. 9:15 pm
Hi Ilija, you made my day! :-)

Somehow I missed the fact that path_info, token and format should be in the GET method... Now that I've changed that, it works like a charm!

Thanks again!
avatar Ilija Studen Staff Jul 30. 2010. 1:44 pm
Hello Casper,

Glad to hear that. Please let us know if there's anything else that we can do for you.
avatar Steve Lilley Nov 9. 2010. 8:52 am
Hey Casper,

is there any chance you could post your newly updated code, so others can see the correct way?

Thanks,

Steve
avatar casper Nov 9. 2010. 6:26 pm
Hi Steve,

Sorry I lost my piece of code, but the clue is in Ilija's comment above. Part of the request needs to be submitted as GET and part as POST:

import urllib2

URL_TO_AC_API = "https://my_ac_site/public/api.php"
TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
PATH_INFO = "/projects/2/milestones"
FORMAT = "xml"

# Put together a request with GET data:
req = urllib2.Request(URL_TO_AC_API+"?path_info="+PATH_INFO+"&token="+TOKEN+"&format="+FORMAT)
# Then add the POST data:
req.add_data("submitted=submitted")

f = urllib2.urlopen(req)
print f.read()


Hope this works for you.
Good luck!
Casper
or Go To Next Page