Search code examples
phpjsonquickblox

Quickblox Token missing error PHP


I am trying to create and retrieve users programatically through quickblox. So far I have done authentication and session creation. But when after this step I try to retrieve users I get token is required error.

Here is the code:

    DEFINE('APPLICATION_ID', 16619);
 DEFINE('AUTH_KEY', "W4EFMSZx3nQZ7eh");
 DEFINE('AUTH_SECRET', "NCp5PyZM9uQvWg4");

 // User credentials
 DEFINE('USER_LOGIN', "removed");
 DEFINE('USER_PASSWORD', "removed");

 // Quickblox endpoints
 DEFINE('QB_API_ENDPOINT', "https://api.quickblox.com");
 DEFINE('QB_PATH_SESSION', "session.json");
 DEFINE('users', "users.json");

 // Generate signature
 $nonce = rand();
 $timestamp = time(); // time() method must return current timestamp in UTC but seems like hi is return timestamp in current time zone
 $signature_string = "application_id=".APPLICATION_ID."&auth_key=".AUTH_KEY."&nonce=".$nonce."&timestamp=".$timestamp."&user[login]=".USER_LOGIN."&user[password]=".USER_PASSWORD;

 echo "stringForSignature: " . $signature_string . "<br><br>";
 $signature = hash_hmac('sha1', $signature_string , AUTH_SECRET);

 // Build post body
 $post_body = http_build_query(array(
                 'application_id' => APPLICATION_ID,
                 'auth_key' => AUTH_KEY,
                 'timestamp' => $timestamp,
                 'nonce' => $nonce,
                 'signature' => $signature,
                 'user[login]' => USER_LOGIN,
                 'user[password]' => USER_PASSWORD
                 ));




 // Configure cURL
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, QB_API_ENDPOINT . '/' . QB_PATH_SESSION); // Full path is - https://api.quickblox.com/session.json
 curl_setopt($curl, CURLOPT_POST, true); // Use POST
 curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body); // Setup post body
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Receive server response
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

 // Execute request and read response
 $response = curl_exec($curl);

 // Check errors
 if ($response) {
         $cu = curl_init();
 curl_setopt($cu, CURLOPT_URL, QB_API_ENDPOINT . '/' . users); 
  curl_setopt($cu, CURLOPT_RETURNTRANSFER, true); 
 curl_setopt($cu, CURLOPT_SSL_VERIFYPEER, FALSE);
 $res = curl_exec($cu);
 echo $res;

 }

I am new to json and curl therefore any help will be highly appreciated


Solution

  • Session creation request returns a session token which you must use in each further request.

    Just set the token and it will work