7

There is all the relevant information present in broken form in the following links on owncloud related websites and from stackoverflow itself:

  1. User Provisioning Api - Owncloud
  2. PHP + curl, HTTP POST sample code
  3. Create user on ownCloud using Ajax Jquery
  4. User Provisioning - php Authentication error

I am trying to do something very simple :

  1. I have setup an owncloud server in my localhost,
  2. I have an html page that takes in string values of user name and password
  3. 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 ?

3 Answers 3

2

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>
&lt;?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
Sign up to request clarification or add additional context in comments.

Comments

1

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).

3 Comments

Thanks for pointing the typo. I fixed it. But the error seems to persist.
Your code is fine now. I'm unfamiliar with OwnCloud but my guess is that either the credentials are incorrect or the user is not allowed to access the API.
Adding the line has no effect on the output since I still get unauthorized response. And you are right someone well versed with own Cloud API can help. And thanks for your response.
0

As your linked post (Create user on Owncloud) shows you need to an basic auth header with some credentials. You need to provide the admin credentials, afaict.

1 Comment

Yes I am trying to understand the settings, credentials etc. But I am not sure what is the correct way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.