Seafile & PHP

481 views
Skip to first unread message

Michelangelo Turillo

unread,
Mar 8, 2014, 6:53:42 AM3/8/14
to sea...@googlegroups.com
Hi Guys,

I am a Zend Framework developer and I am trying to create a module for the Seafile Service for the community. I need to know why I get the error:

 {"detail": "You do not have permission to perform this action."}

I have called the Url as mentioned in the WebApi documentation https://github.com/haiwen/seafile/wiki/Seafile-web-API#wiki-new-directory but I get that error.

This is a simple php code to describe my action:

$postData = array('operation' => 'mkdir', 'repo-id' => $repoid, 'token' => self::$token, 'p' => $remoteDir);
$data = self::request ( "$url/api2/repos/$repoid/dir/", true, $postData );
 
 So the result is that the $data variable is :  {"detail": "You do not have permission to perform this action."}

08-03-2014 12:51:12] https://cloud.seafile.com//api2/auth-token/
[08-03-2014 12:51:12]
array (
  'username' => 'in...@mydomain.com',
  'password' => 'thisismypassword',
)[08-03-2014 12:51:23] https://cloud.seafile.com//api2/repos/693c5bee-438f-42ba-b2dc-1fb13c82fd44/dir/
[08-03-2014 12:51:23]
array (
  'operation' => 'mkdir',
  'repo-id' => '693c5bee-438f-42ba-b2dc-1fb13c82fd44',
  'token' => 'b7fd611e119b49ea1c27405c41bf584e1e80267e',
  'p' => '/documents/2013/Invoices/2/May/',
)

Where is the error?

thanks

Tom C.

unread,
Mar 10, 2014, 5:38:24 AM3/10/14
to sea...@googlegroups.com
I created a quick and dirty version of a file upload using the RestCurlClient Class from davidmpaz ( https://github.com/davidmpaz/rest-curlclient-php/blob/master/rest_curl_client.php ) a while ago...
Should be almost self explaining:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'rest_curl_client.php');
$c = new RestCurlClient();
$json_auth_token = json_decode($c->post('http://<your_url>:8000/api2/auth-token/',
array('username'=>'m...@email.com', 'password'=>'test123')));
echo 'Token: <pre>';
print_r($json_auth_token);
$token = (string)$json_auth_token->token;
$json_repos = json_decode($c->get('http://<your_url>:8000/api2/repos/', array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token))
));
echo '</pre>Repos:<pre>';
print_r($json_repos);

$upload_link = json_decode($c->get('http://<your_url>:8000/api2/repos/'.$json_repos[0]->id.'/upload-link/', array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token))
));
echo '</pre>Upload Link:<pre>';
var_dump($upload_link);
$uploadhash = $c->post($upload_link, array('file'=>new CurlFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'test.php', 'text/plain', 'test.php'), 'filename'=>'test.php', 'parent_dir'=>'/'), array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token, 'Expect:')));

echo '</pre>Upload hash:<pre>';
print_r($uploadhash);
?> 

I hope this helps.

Michelangelo Turillo

unread,
Mar 10, 2014, 5:39:37 AM3/10/14
to sea...@googlegroups.com
Hi Tom,

thanks for your code. I will test this evening.

regards
--
You received this message because you are subscribed to a topic in the Google Groups "seafile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/seafile/d7XPqpmVrvY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to seafile+u...@googlegroups.com.
To post to this group, send email to sea...@googlegroups.com.
Visit this group at http://groups.google.com/group/seafile.
For more options, visit https://groups.google.com/d/optout.

Michelangelo Turillo

unread,
Mar 10, 2014, 5:43:53 AM3/10/14
to sea...@googlegroups.com
Hi Tom,

I am trying to understand where you create the directory in your code. Where is the command to create the directory in the Seafile server?

thanks

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'rest_curl_client.php');
$c = new RestCurlClient();
$json_auth_token = json_decode($c->post('http://<your_url>:8000/api2/auth-token/',
array('username'=>'m...@email.com', 'password'=>'test123')));
echo 'Token: <pre>';
print_r($json_auth_token);
$token = (string)$json_auth_token->token;
$json_repos = json_decode($c->get('http://<your_url>:8000/api2/repos/', array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token))
));
echo '</pre>Repos:<pre>';
print_r($json_repos);

$upload_link = json_decode($c->get('http://<your_url>:8000/api2/repos/'.$json_repos[0]->id.'/upload-link/', array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token))
));
echo '</pre>Upload Link:<pre>';
var_dump($upload_link);
$uploadhash = $c->post($upload_link, array('file'=>new CurlFile(dirname(__FILE__).DIRECTORY_SEPARATOR.'test.php', 'text/plain', 'test.php'), 'filename'=>'test.php', 'parent_dir'=>'/'), array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token, 'Expect:')));

echo '</pre>Upload hash:<pre>';
print_r($uploadhash);
?> 

I hope this helps.

Tom C.

unread,
Mar 10, 2014, 6:17:40 AM3/10/14
to sea...@googlegroups.com
Try this one to create a directory:

$dirpath = '/your_directory_name';
$json_auth_token = json_decode($c->post('http://<your_url>:8000/api2/repos/'.$json_repos[0]->id.'/dir/?p='.$dirpath,
array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token), CURLOPT_POSTFIELDS => serialize(array('operation'=>'mkdir')))));

I think the main problem you have with your code is, that you're missing to send the Token using the Authorization Header.

Tom C.

unread,
Mar 10, 2014, 6:52:49 AM3/10/14
to sea...@googlegroups.com
Sorry there was an error in the code. This is the fixed one, based on the RestCurlClient Class:

$dirpath = '/your_directory_name';
$data = json_decode($c->post('http://<your_url>:8000/api2/repos/'.$json_repos[0]->id.'/dir/?p='.$dirpath,
array('operation'=>'mkdir'), array(CURLOPT_HTTPHEADER=>array('Authorization: Token '.$token))));

So, first create the url you want to post to e.g.
http://<your_url>:8000/api2/repos/<repo-id>/dir/?p=/<your_new_directory_name>
Then set the post parameter key 'operation' with value 'mkdir'.
Afterwards add the entry 'Authorization: Token <your_auth_token>' to the headers being sent. <- This step is mandatory for every request!
Execute the POST request.
If everything goes well, you should receive the message 'success' from the api.

Alessandro Balestra

unread,
Oct 25, 2015, 10:31:38 AM10/25/15
to seafile
Hi Michelangelo, i have the same problem creating a directory, i try with the code of Tom but still the same errore, did you find the solution?
Thanks
Reply all
Reply to author
Forward
0 new messages