ForumsDevelopersCan't get PHP POST to work


Can't get PHP POST to work
Author Message
jensbn

Posted: Nov 04, 2015
Score: 0 Reference
I'd trying to add new tasks using a PHP form. I'm using the sample code from the help (http://api.toodledo.com/3/ ). I can get authentication and GET requests to work, but not POST requests which I need for adding tasks. I've been trying all sorts of things. At best I get the errorcode "Forbidden", but I have no idea why. .

Here's a bit of extra information

If I use the URLs (in adapted form) from the API playground I get the same error.

My post request looks like this in PHP :
$data = $toodledo->postResource("http://api.toodledo.com/3/tasks/add.php",$access_token,'tasks=["title":"TES TADD1"]');

in my modified Oauth file the postResource function gives :
'{"errorCode":2,"errorDesc":"Forbidden","errors":[{"status":"2","message":"Forbidden"}]}'

using the original Oauth2 file the postResource function gives:

'{"errorCode":1,"errorDesc":"No access_token given"}'

This looks to me like an error in the "official" code sample.

For reference, I here post the function I use in the toodledo_oauth2.php sample script:

public function postResource($resource_url,$access_token,$post) {
//original line $url = $resource_url; (added access token)
$url = $resource_url;

//if post is a query string it does a application/x-www-form-urlencoded post
//if post is an array it does a multipart/form-data post
//access_token cannot be in body of multipart post, so we put it in the url in this case
if(is_array($post)) {
$url .= "?access_token=".$this->accessToken;
} else {
$post.= "&access_token=".$this->accessToken;
}

$ci = curl_init();
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_POST, TRUE);
curl_setopt($ci, CURLOPT_POSTFIELDS, $post);
curl_setopt($ci, CURLOPT_URL, $url);
$response = curl_exec($ci);
curl_close ($ci);

return $response;
}

Hope to get some help!

Regards,

Jens
Jake

Toodledo Founder
Posted: Nov 04, 2015
Score: 0 Reference
I see one problem with your code. You are passing a string as the $post variable:

$post = 'tasks=["title":"TEST ADD1"]'

This is supposed to be a task object in JSON format (or an array of task objects), but you are sending an array of fields. You'll want something like this instead:

$post = 'tasks={"title":"TEST ADD1"}'
or
$post = 'tasks=[{"title":"TEST ADD1"},{"title":"TEST ADD2"}]'

You could also try sending the data as an array instead of a string. This is how we do it in our automated tests:

$task = array("title"=>"Title");
$post = array("tasks"=>json_encode($task));
$data = $this->postResource("tasks/add.php",$post);
jensbn

Posted: Nov 05, 2015
Score: 0 Reference
Thanks. I tried changing the brackets but I still get the error message forbidden. Could it be that I'm somehow not authorized to POST data? That would also explain why it fails when I use minimally modified sample requests from the API playground.
Jake

Toodledo Founder
Posted: Nov 06, 2015
Score: 0 Reference
No, we dont block people from doing posts. I am pretty sure the problem is the encoding of the data you are sending along with the post.
jensbn

Posted: Nov 07, 2015
Score: 0 Reference
Thanks, but hours further, I still can't get it to work. Are you using the same Postresource function as is in the sample in the repository? I wonder if that one is functional at all. Is that verified working?
Jake

Toodledo Founder
Posted: Nov 09, 2015
Score: 0 Reference
We use the same code, but I did just notice what the error probably is.

You are passing the access token into the post function as a variable, but if you look inside the function, that variable is ignored. Instead it uses the class's variable with the same name. This is a bug in our example code. Very sorry about this. There are two fixes possible:

1) Modify the function to use $accessToken instead of $this->accessToken
2) Set your access token on the class before calling the function.

$toodledo = new Toodledo_OAuth2();
$toodledo->accessToken = "xyz";
$toodledo->postResource($url,'',$post);

We do #2 in our code, which is why it worked for us.
jensbn

Posted: Nov 10, 2015
Score: 0 Reference
Thanks. It works now. I fixed the variable name by setting it to $access_token in the OAuth2 file. I found another problem too, which may explain the 'forbidden' message. In OAuth2 var $scope = was not set to include 'write'. If you correct the first error in the example code you might want to add a comment at the $scope variable to set it to write if you want full functionality.
You cannot reply yet

U Back to topic home

R Post a reply

To participate in these forums, you must be signed in.