I want to use the REST API to Share to create Share sites.I can log into alfresco with this code :use warnings;
use strict;
use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;
use JSON;
my $alfTicket;
my $host = 'localhost';
my $port = 8080;
sub login ()
{
my ($username, $password) = @_;
my $ua = LWP::UserAgent->new;
my $h = HTTP::Headers->new;
$h->header ('Content-Type' => 'application/json', Accept=>'application/json');
my $GetReq = HTTP::Request->new
(
'POST',
"http://$host:$port/alfresco/service/api/login",
$h,
'{"username" : "' . $username . '","password" : "' . $password . '"}'
);
my $res = $ua->request($GetReq);
# Check the outcome of the response
if ($res->is_success)
{
print "Response :\n";
print $res->as_string;
print "\n\n**************************\n\n";
my $hashRef = JSON::decode_json ($res->content);
$alfTicket = $hashRef->{"data"}->{"ticket"};
return 1;
}
else
{
print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
print "\t" . $res->content . "\n";
return 0;
}
}
print "username: ";
my $username = <STDIN>;
chomp ($username);
print "password: ";
my $password = <STDIN>;
chomp ($password);
if (&login ($username, $password))
{
print "$username is logged in !\n";
}
else
{
print "$username can't log in $host:$port\n";
}Response :HTTP/1.1 200 OKCache-Control: no-cacheConnection: closeDate: Wed, 07 Oct 2009 14:03:08 GMTPragma: no-cacheServer: Apache-Coyote/1.1Content-Type: application/json;charset=UTF-8Client-Date: Wed, 07 Oct 2009 14:03:08 GMTClient-Peer: 130.190.227.201:8080Client-Response-Num: 1{ "data": { "ticket":"TICKET_596cd264bc9bfa3492777bc13e4dd3cacabe0c41" }}In result I get a ticket back.Further I can use this ticket to acces other REST API functions.But I can't use It to create a site with POST /share/modules/create-site with this code :use warnings;
use strict;
use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;
use JSON;
my $alfTicket;
my $host = 'localhost.fr';
my $port = 8080;
sub login ()
{
my ($username, $password) = @_;
my $ua = LWP::UserAgent->new;
my $h = HTTP::Headers->new;
$h->header ('Content-Type' => 'application/json', Accept=>'application/json');
my $GetReq = HTTP::Request->new
(
'POST',
"http://$host:$port/alfresco/service/api/login",
$h,
'{"username" : "' . $username . '","password" : "' . $password . '"}'
);
my $res = $ua->request($GetReq);
# Check the outcome of the response
if ($res->is_success)
{
print "Response login :\n";
print $res->as_string;
print "\n\n**************************\n\n";
my $hashRef = JSON::decode_json ($res->content);
$alfTicket = $hashRef->{"data"}->{"ticket"};
return 1;
}
else
{
print 'Error (' . $res->{_rc} . ') : ' . $res->{_msg} . "\n";
print "\t" . $res->content . "\n";
return 0;
}
}
sub createShareSite ()
{
my ($siteInfosRef) = @_;
my $content = JSON::encode_json ($siteInfosRef);
my $ua = LWP::UserAgent->new;
my $h = HTTP::Headers->new;
$h->header ('Content-Type' => 'application/json', Accept=>'application/json');
my $GetReq = HTTP::Request->new
(
'POST',
"http://$host:$port/share/service/modules/create-site?alf_ticket=$alfTicket",
$h,
$content
);
print "—————————-\n";
print "Request createShareSite :\n" . $GetReq->as_string . "\n";
print "—————————-\n";
my $res = $ua->request($GetReq);
print "\n\n**************************\n\n";
print "Response createShareSite :\n";
print $res->as_string;
print "\n\n**************************\n\n";
if ($res->is_success)
{
print $res->content;
print "\n";
return 1;
}
else
{
return 0;
}
}
print "username: ";
my $username = <STDIN>;
chomp ($username);
print "password: ";
my $password = <STDIN>;
chomp ($password);
if (&login ($username, $password))
{
print "$username is logged in !\n";
my %siteInfos;
$siteInfos {'shortName'} = 'ferbru60';
$siteInfos {'title'} = 'The ferbru60 site !';
$siteInfos {'description'} = 'This is the ferbru60 site.';
$siteInfos {'visibility'} = 'PRIVATE';
$siteInfos {'sitePreset'} = 'site-dashboard';
if (&createShareSite (\%siteInfos))
{
print "It's rocks !\n";
}
else
{
print "bad news\n";
}
}
else
{
print "$username can't log in $host:$port\n";
}I gotResponse login :HTTP/1.1 200 OKCache-Control: no-cacheConnection: closeDate: Wed, 07 Oct 2009 14:26:20 GMTPragma: no-cacheServer: Apache-Coyote/1.1Content-Type: application/json;charset=UTF-8Client-Date: Wed, 07 Oct 2009 14:26:20 GMTClient-Peer: 130.190.227.201:8080Client-Response-Num: 1{ "data": { "ticket":"TICKET_86d8f014c5b0b5bb6822542a848b9024ef04d4d1" }}**************************ferbru60 is logged in !—————————-Request createShareSite :POST http://localhost:8080/share/service/modules/create-site?alf_ticket=TICKET_86d8f014c5b0b5bb6822542a848b9024ef04d4d1Accept: application/jsonContent-Type: application/json{"visibility":"PRIVATE","sitePreset":"site-dashboard","title":"The ferbru60 site !","shortName":"ferbru60","description":"This is the ferbru60 site."}—————————-**************************Response createShareSite :HTTP/1.1 401 Non-Autoris´+¢Cache-Control: no-cacheConnection: closeDate: Wed, 07 Oct 2009 14:26:20 GMTPragma: no-cacheServer: Apache-Coyote/1.1Content-Type: application/json;charset=UTF-8Client-Date: Wed, 07 Oct 2009 14:26:20 GMTClient-Peer: 130.190.227.201:8080Client-Response-Num: 1Client-Warning: Missing Authenticate header{ "status" : { "code" : 401, "name" : "Unauthorized", "description" : "The request requires HTTP authentication." }, "message" : "error.loggedOut", "exception" : "", "callstack" : [ ], "server" : "Alfresco Community v3.2.0 (2039) schema 2-á019", "time" : "7 oct. 2009 16:26:20"}**************************bad newsIt's really a bad news.According to the error I've tried that :use warnings;
use strict;
use HTTP::Request::Common;
use HTTP::Request;
use HTTP::Response;
use HTTP::Headers;
use LWP::UserAgent;
use JSON;
my $alfTicket;
my $host = 'localhost';
my $port = 8080;
sub createShareSite ()
{
my ($username, $password, $siteInfosRef) = @_;
my $content = JSON::encode_json ($siteInfosRef);
my $ua = LWP::UserAgent->new;
my $h = HTTP::Headers->new;
$h->header ('Content-Type' => 'application/json', Accept=>'application/json');
$h->authorization_basic ($username, $password);
my $GetReq = HTTP::Request->new
(
'POST',
"http://$host:$port/share/service/modules/create-site",
$h,
$content
);
print "—————————-\n";
print "Request createShareSite :\n" . $GetReq->as_string . "\n";
print "—————————-\n";
my $res = $ua->request($GetReq);
print "\n\n**************************\n\n";
print "Response createShareSite :\n";
print $res->as_string;
print "\n\n**************************\n\n";
if ($res->is_success)
{
print $res->content;
print "\n";
return 1;
}
else
{
return 0;
}
}
print "username: ";
my $username = <STDIN>;
chomp ($username);
print "password: ";
my $password = <STDIN>;
chomp ($password);
my %siteInfos;
$siteInfos {'shortName'} = 'ferbru60';
$siteInfos {'title'} = 'The ferbru60 site !';
$siteInfos {'description'} = 'This is the ferbru60 site.';
$siteInfos {'visibility'} = 'PRIVATE';
$siteInfos {'sitePreset'} = 'site-dashboard';
if (&createShareSite ($username, $password, \%siteInfos))
{
print "It's rocks !\n";
}
else
{
print "bad news\n";
}
And I get that :—————————-Request createShareSite :POST http://localhost:8080/share/service/modules/create-siteAccept: application/jsonAuthorization: Basic ZmVycmFyaWI6ZmVycmFyaWI=Content-Type: application/json{"visibility":"PRIVATE","sitePreset":"site-dashboard","title":"The ferbru60 site !","shortName":"ferbru60","description":"This isthe ferbru60 site."}—————————-**************************Response createShareSite :HTTP/1.1 401 Non-Autoris´+¢Cache-Control: no-cacheConnection: closeDate: Wed, 07 Oct 2009 14:32:42 GMTPragma: no-cacheServer: Apache-Coyote/1.1Content-Type: application/json;charset=UTF-8Client-Date: Wed, 07 Oct 2009 14:32:42 GMTClient-Peer: 130.190.227.201:8080Client-Response-Num: 1Client-Warning: Missing Authenticate header{ "status" : { "code" : 401, "name" : "Unauthorized", "description" : "The request requires HTTP authentication." }, "message" : "error.loggedOut", "exception" : "", "callstack" : [ ], "server" : "Alfresco Community v3.2.0 (2039) schema 2-á019", "time" : "7 oct. 2009 16:32:42"}**************************bad newsIt's really a bad news.What I've missed ?