03-03-2016 01:08 AM
Hi ,
I trying to create a site in alfresco share using a webscript in java and this i am doing with the help of below code:
HttpClient client = new HttpClient();
PostMethod httpPost = new PostMethod ("http://localhost:8080/share/service/modules/create-site?authority="+username+"&alf_ticket="+alf_tkt);
JSONObject site = new JSONObject();
try {
site.put("shortName", "java_test");
site.put("Visiblity", "Public");
site.put("sitePreset", "site-dashboard");
site.put("title", "java_test");
site.put("description", "test");
httpPost.setDoAuthentication(true);
httpPost.setRequestHeader("Content-Type", "application/json");
httpPost.setRequestEntity( new StringRequestEntity(site.toString(), "application/json", "UTF-8"));
int status1 = client.executeMethod(httpPost);
if (status1 != HttpStatus.SC_OK) {
System.err.println("Method failed: " + httpPost.getStatusLine());
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
It is giving me the error as HTTP/1.1 401 Unauthorized.
Please help me in this.
Regards,
Nancy
03-03-2016 03:15 AM
Hi Nancy, Please make sure your ticket is not expired and you are calling Share script which again internally calls Alfresco data script to create a site and that script also required authentication. So I believe here authentication chain is breaking.
Why dont you directly call Alfresco data script. Just change your script URL to data script as below and it should work.
PostMethod httpPost = new PostMethod ("http://localhost:8080/alfresco/service/api/sites?&alf_ticket="+alf_tkt);
Niketa Patel
Senior Alfresco Consultant
CIGNEX Datamatics
03-03-2016 03:50 AM
03-16-2016 02:04 AM
03-21-2016 03:29 AM
04-07-2016 07:37 AM
04-08-2016 01:51 AM
04-13-2016 03:03 AM
04-14-2016 02:14 AM
HttpClient http = new HttpClient();
PostMethod loginPost = new PostMethod("http://localhost:8080/share/page?pt=login");
String logindata = "username="+user+"&password="+alf_tkt;
loginPost.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
loginPost.setRequestEntity(new StringRequestEntity(logindata, "text/plain", "UTF-8"));
System.out.println("dologin script status: " + http.executeMethod(loginPost));
org.apache.commons.httpclient.Header[] headers = loginPost.getResponseHeaders();
Header cookies1 =loginPost.getResponseHeader("Set-Cookie");
// getting JSESSIONID from the header
System.out.println("session id ============" +sessionid);
GetMethod authenticationGet = new GetMethod("http://localhost:8080/share/service/modules/authenticated?a=user");
authenticationGet.setRequestHeader("JSESSIONID", sessionid);
System.out.println("User is authenticated script status : " + http.executeMethod(authenticationGet));
PostMethod httpPost = new PostMethod ("http://localhost:8080/share/service/modules/create-site");
JSONObject site = new JSONObject();
try{
site.put("shortName", "java");
site.put("Visiblity", "PUBLIC");
site.put("sitePreset", "site-dashboard");
site.put("title", "java");
site.put("description", "Google-cURL_next");
httpPost.setRequestHeader("Content-Type", "application/json");
httpPost.setRequestHeader("Accept", "application/json");
httpPost.setRequestEntity(new StringRequestEntity(site.toString(), "application/json", "UTF-8"));
int status = http.executeMethod(httpPost);
if (status != HttpStatus.SC_OK) {
System.err.println("Method failed: " + httpPost.getStatusLine());
}
else {
System.out.println("site created successfully");
}
04-15-2016 02:28 AM
//PostMethod loginPost = new PostMethod("http://localhost:8080/share/page?pt=login");
PostMethod loginPost = new PostMethod("http://localhost:8080/share/page/dologin"); //USE THIS ONE
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.