cancel
Showing results for 
Search instead for 
Did you mean: 

Issue in logging out user from Java - programmatically

pradeepsimha
Champ in-the-making
Champ in-the-making
Hi Team,

I am trying to logout a user programmatically using the following REST API:

http://localhost:8080/alfresco/service/api/login/ticket/TICKET_<value>"

But from Java I am getting following error:

<code>Failed : HTTP error code : 401:

But if I try to hit the url directly from the browser, it asks for Admin user login, so I provide that and it will work perfectly. I know before deleting a ticket I need to authenticate using Admin user, but I am trying to figure out how to do this programmatically? Before I delete a user should I again login using Admin user? Or is there anyway? Because help docs doesn't specify how to do this.

Can someone kindly help me in this?
3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator
This webscript needs have user authentication requirements ,that means if you want to execute this webscript, a named user authentication is required.
if you want to logout yourself you can login use your identification an execute this webscript.
If you want to logout other user you should login as admin

Following is example code you can refer to
import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.DeleteMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.json.JSONObject;public class LoginAddLogoutTest {   public String login() {      String ticket = null;      HttpClient client = new HttpClient();      String apiurl = "http://localhost:8080/alfresco/service/api/login";      PostMethod post = new PostMethod(apiurl);      try {         JSONObject login = new JSONObject();         login.put("username", "admin");         login.put("password", "admin");         post.setDoAuthentication(true);         post.setRequestHeader("Content-Type", "application/json");         post.setRequestEntity(new StringRequestEntity(login.toString(),               "application/json", "UTF-8"));         int status = client.executeMethod(post);         if (status != HttpStatus.SC_OK) {            System.err.println("Method failed: " + post.getStatusLine());         }         String responseData = post.getResponseBodyAsString();         System.out.println(responseData);         JSONObject response = new JSONObject(responseData);         ticket = response.getJSONObject("data").getString("ticket");      } catch (Exception e) {         e.printStackTrace();      } finally {         post.releaseConnection();      }      return ticket;   }   public void logout(String ticket, String tologoutTicket) {      HttpClient client = new HttpClient();      String apiurl = "http://localhost:8080/alfresco/service/api/login/ticket/"            + tologoutTicket + "?alf_ticket=" + ticket;      DeleteMethod post = new DeleteMethod(apiurl);      try {         post.setDoAuthentication(true);         post.setRequestHeader("Content-Type", "application/xml");         int status = client.executeMethod(post);         if (status != HttpStatus.SC_OK) {            System.err.println("Method failed: " + post.getStatusLine());         }         String resultString = post.getResponseBodyAsString();         System.out.println(resultString);      } catch (Exception e) {         e.printStackTrace();      } finally {         post.releaseConnection();      }   }   public static void main(String[] args) {      LoginAddLogoutTest t = new LoginAddLogoutTest();      String ticket = t.login();      t.logout(ticket, ticket); // in my case I logout admin using admin                           // authentication   }}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍






Thank you very much Smiley Happy