Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to logout twitter account by deleting cookies?

I want to logout my twitter account by deleting the cookies created by it. I am able to retrive the cookies created by twitter using code:

String twit_cookie = getCookie ("http://www.twitter.com");

But how can i delete only cookies created by twitter because removeAllCookie() deletes all the cookies created by browser. How can i delete the specific cookie by URL or by name???

Please help...

like image 884
Rajat Avatar asked Jan 18 '12 10:01

Rajat


2 Answers

CookieManager class has a method setCookie. Have you tried it like:

setCookie("http://www.twitter.com", null);

Or perhaps

setCookie("http://www.twitter.com", "auth_token=''");
like image 109
Simas Avatar answered Oct 03 '22 03:10

Simas


You can use the method CookieManager#setCookie(String url, String value). As stated in the docs:

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie.

The "clearest" way is to set all cookies created by twitter to expired (a time in the past). The code from this answer is almost right, except the date is in the future.
Modified code:

final String domain = "http://www.twitter.com";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
String cookiestring = cookieManager.getCookie(domain); //get all cookies
String[] cookies =  cookiestring.split(";");
for (int i=0; i<cookies.length; i++) {
    String[] cookieparts = cookies[i].split("="); //split cookie into name and value etc.
    // set cookie to an expired date
    cookieManager.setCookie(domain, cookieparts[0].trim()+"=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
}
CookieSyncManager.getInstance().sync(); //sync the new cookies just to be sure
like image 21
Manuel Allenspach Avatar answered Oct 03 '22 04:10

Manuel Allenspach