Issue in logging out user from Java - programmatically

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2014 06:48 AM
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?
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?
Labels:
- Labels:
-
Archive
3 REPLIES 3
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2014 07:47 AM
Can't you embed your java code within AuthenticationUtil.runAs block ?
https://forums.alfresco.com/forum/developer-discussions/alfresco-explorer-development/action-setruna...
http://docs.alfresco.com/3.4/index.jsp?topic=%2Fcom.alfresco.Enterprise_3_4_0.doc%2Freferences%2Fapi...
https://forums.alfresco.com/forum/developer-discussions/alfresco-explorer-development/action-setruna...
http://docs.alfresco.com/3.4/index.jsp?topic=%2Fcom.alfresco.Enterprise_3_4_0.doc%2Freferences%2Fapi...
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2014 10:26 AM
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
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 }}

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2014 12:52 PM
Thank you very much

