There is all the relevant information present in broken form in the following links on owncloud related websites and from stackoverflow itself:
I am trying to do something very simple :
I send the page request to be processed by the following php script.
<?php echo "Begun processing credentials , first it will be stored in local variables" . "<br/>"; // Loading into local variables $userName = $_POST['username']; $RRpassword = $_POST['password']; echo "Hello " . $userName . "<br/>"; echo "Your password is " . $RRpassword . "<br/>"; // Add data, to owncloud post array and then Send the http request for creating a new user $ownCloudPOSTArray = array('username' => $userName, 'password' => $RRpassword ); $url = 'http://localhost/owncloud/ocs/v1.php/cloud/users'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo "<br/>Created a new user in owncloud"; ?>
I get the output like:
Begun processing credentials , first it will be stored in local variables
Hello Frank
Your password is frankspassword
failure 997 Unauthorised
Created a new user in owncloud
I also tried to login to own cloud using following php script:
// Login As Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ownAdminPassword';
$ch = curl_init('http://localhost/owncloud');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$ownAdminname:$ownAdminpassword");
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Even this one fails.
So in short it doesn't work. I am also unable to login via similar script to owncloud. What is the proper way to do this ? What settings am I missing ? Can someone help please ?
Since this question pertains to owncloud specifically, I created an account and posted a question linking this one to it in owncloud forum.
There I was suggested by the owncloud master @RealRancor, the following,
Just had another look, maybe its just easy to replace:
$url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';
with
$url = 'http://adminuser:adminpass@localhost/owncloud/ocs/v1.php/cloud/users';
as shown in the documentation.
And amazingly it worked like a charm. So here is the entire modified php script:
<pre>
<?php
echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";
// Loading into local variables
$userName = $_POST['username'];
$RRpassword = $_POST['password'];
echo "Hello " . $userName . "<br/>";
echo "Your password is " . $RRpassword . "<br/>";
// Login Credentials as Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ufi2016%%';
// Add data, to owncloud post array and then Send the http request for creating a new user
$url = 'http://' . $ownAdminname . ':' . $ownAdminpassword . '@localhost/owncloud/ocs/v1.php/cloud/users';
echo "Created URL is " . $url . "<br/>";
$ownCloudPOSTArray = array('userid' => $userName, 'password' => $RRpassword );
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Created a new user in owncloud<br/>";
// Add to a group called 'Users'
$groupUrl = $url . '/' . $userName . '/' . 'groups';
echo "Created groups URL is " . $groupUrl . "<br/>";
$ownCloudPOSTArrayGroup = array('groupid' => 'Users');
$ch = curl_init($groupUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArrayGroup);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Added the new user to default group in owncloud";
?>
</pre>
And here is the output:
Begun processing credentials , first it will be stored in local variables
Hello Frank
Your password is frankspassword
Created URL is http://ownAdmin:ufi2016%%@localhost/owncloud/ocs/v1.php/cloud/users
Response from curl : ok 100
Created a new user in owncloud
Created groups URL is http://ownAdmin:ufi2016%%@localhost/owncloud/ocs/v1.php/cloud/users/Frank/groups
Response from curl : ok 100
Added the new user to default group in owncloud
The owncloud documentation states that authentication is done by means of a basic HTTP Authentication header. What you are currently doing is sending the credentials as parameters to the API call. You need to add the following line:
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $RRpassword);
There's also a typo in CURLOPT_RETURNTRANSFER ($curl
instead of $ch
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With