LSRC2 add_participants failing with invalidSessionKey

More
2 years 10 months ago - 2 years 10 months ago #117344 by lauriek334
lauriek334 created the topic: LSRC2 add_participants failing with invalidSessionKey
I've been trying to add a participant to a survey using LSRC2 and keep getting the following JSON return object:
{"id":"1","result":{"status":"Invalid session key"},"error":null}
I definitely get a correct session key using get_session_key
I then can use the returned session key to get a list of groups and questions about a survey.
I can list the participants for a given survey using the session key.
When I try to add a participant, I get the "invalid session key" message. Here's the JSON object that is being passed to LimeSurvey:
{"params":{"iSurveyID":872266,"sSessionKey":"6vw6zymuhdw2xg3be7pydwqhptqp24ix","participantData":[{"email":"cotwins@cotwins.org","firstname":"334","lastname":"334"}],"createTokenKey":true},"id":"1","method":"add_participants"}
I am at a loss what to do next.
Is there a particular way that a survey has to be set up in order to add participants using the remote control API?
FYI. I am developing a Python class to manage all my remote control interactions with the LimeSurvey instance which is running on a centos VM.
Last Edit: 2 years 10 months ago by lauriek334. Reason: added old param line

Please Log in to join the conversation.

More
2 years 10 months ago #117345 by lauriek334
lauriek334 replied the topic: LSRC2 add_participants failing with invalidSessionKey
Just to clarify - I know I have a valid session key because I am able to perform the following tasks using the API:
- list_surveys
- list_groups for a given survey
- list_questions for a given group in a survey
- list_participants for a given survey

Please Log in to join the conversation.

More
2 years 10 months ago #117347 by lauriek334
lauriek334 replied the topic: LSRC2 add_participants failing with invalidSessionKey
I added 2 error_log call to application/libraries/LSjsonRPCServer.php in the handle routine and to application/helpers/remotecontrol/remotecontrol_handle.php at the start of the add_participants routine and found that the sessionkey parameter and the surveyID paramenter are being reversed. Here's the output that I logged:
[Mon Feb 16 17:56:24.795653 2015] [:error] [pid 813] [client **************] Array  = {"id":"1","method":"add_participants","params":{"iSurveyID":872266,"sSessionKey":"k82ajycezixy3t3jrkt78w5cdhjq9zp6","participantData":[{"email":"cotwins@cotwins.org","lastname":"334","firstname":"334"}],"createTokenKey":true}}
[Mon Feb 16 17:56:24.795781 2015] [:error] [pid 813] [client **************] add_participants:: ENTERED: sessionkey = 872266, iSurveyID = k82ajycezixy3t3jrkt78w5cdhjq9zp6, participant data: [{"email":"cotwins@cotwins.org","lastname":"334","firstname":"334"}]
I'm not a php expert and am still figuring out how the input params are unpacked and passed to the add_participants routine.
Here's the error_log call I placed inside the add_participants routine:
public function add_participants($sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken=true)
    {
        error_log("======== add_participants:: ENTERED: sessionkey = " . $sSessionKey . ", iSurveyID = " . $iSurveyID . ", participant data: " . json_encode($aParticipantData));

Am I missing something - seems like there is a bug here?

Please Log in to join the conversation.

More
2 years 10 months ago #117355 by lauriek334
lauriek334 replied the topic: LSRC2 add_participants failing with invalidSessionKey
I just tried to use get_participant_properties and am also receiving the "invalid Session key" error. I added logging to the get_participant_properties routine in application/helpers/remotecontrol/remotecontrol_handle.php and get the following output:
[Mon Feb 16 23:33:58.127903 2015] [:error] [pid 814] [client #############] ======== get_participant_properties:: ENTERED: sessionkey = 10, iSurveyID = Array,i iSurveyID = Array, aTokenProperties: 872266
This shows the parameters are messed up by the time the get_participant_properties routine is called.

Here's the matching output from inside the handle routine in /var/www/html/limesurvey151302/application/libraries/LSjsonRPCServer.php.
[Mon Feb 16 23:33:58.127809 2015] [:error] [pid 814] [client #############] ======== Array  = {"params":{"iTokenID":"10","aTokenProperties":["tid"],"sSessionKey":"sdw4532jut63d9c3ddu76qnwbgrq73uy","iSurveyID":872266},"method":"get_participant_properties","id":"1"}
[Mon Feb 16 23:33:58.127853 2015] [:error] [pid 814] [client #############] ======== Array  method = "get_participant_properties"
[Mon Feb 16 23:33:58.127871 2015] [:error] [pid 814] [client #############] ======== Array  params = {"iTokenID":"10","aTokenProperties":["tid"],"sSessionKey":"sdw4532jut63d9c3ddu76qnwbgrq73uy","iSurveyID":872266}
This shows that again the parameters appear to be fine when the handle routine is called but get completely mixed up by the time the actual routine is called.
I think I am doing something incorrect but I cannot figure out what.

Please Log in to join the conversation.

More
2 years 10 months ago #117360 by DenisChenu
DenisChenu replied the topic: LSRC2 add_participants failing with invalidSessionKey
Hi,

Great, you found a bug and the fix !

manual.limesurvey.org/How_to_contribute_new_features

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
2 years 10 months ago - 2 years 10 months ago #117363 by lauriek334
lauriek334 replied the topic: LSRC2 add_participants failing with invalidSessionKey
I don't see how to fix this with the call_user_func_array being used.
I would put in a switch statement and explicitly call each of the rpc functions directly with the parameters explicitly passed for each function call. I realize this is not elegant but it would ensure the functions are called with the arguments correclty ordered. Here is what I am going to try in order to make the add_participants call work for me:
           try {
                $result = false;
                switch ($request['method']) {
                case "add_participants":
 
                        $response = add_participants($request["aSessionKey"], $request["iSurveyID"] $request["aParticipantData"], $request["bCreateToken"]);
                        $result = true;
                        break;
                default:
                        $result = @call_user_func_array(array($object,$request['method']),$request['params']);
                }
Will this work?
Last Edit: 2 years 10 months ago by lauriek334. Reason: removed unecessary error_log calls

Please Log in to join the conversation.

More
2 years 10 months ago #117364 by DenisChenu
DenisChenu replied the topic: LSRC2 add_participants failing with invalidSessionKey
Did you try with:

{"params":{"sSessionKey":"6vw6zymuhdw2xg3be7pydwqhptqp24ix","iSurveyID":872266,"participantData":[{"email":"cotwins@cotwins.org","firstname":"334","lastname":"334"}],"createTokenKey":true},"id":"1","method":"add_participants"}

?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
1 year 1 month ago #143874 by cosimo193
cosimo193 replied the topic: LSRC2 add_participants failing with invalidSessionKey
It's an order issue; I've been struggling with a similar problem over the last couple of days and found your post here.

Basically, the params values in your JSON must be in the order that they are provided in the API definition. Even if you use named association, the JSON-RPC mapping seems to ignore the parameter keys and just pulls out the value in order to pass to the function.

So, what's happening is that the survey ID is being treated as the session key because it's the first item in the params object. Apparently JSON-RPC2 handles named associations properly but that doesn't appear to be what's included in Limesurvey so order is important.

This may also be what DenisChenu was referring to but didn't explicitly say.

My next issue is, when there are multiple optional parameters, how to skip ones I don't want to provide!

Hope this helps.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!