Estoy tratando de mostrar la lista de videos favoritos de un usuario en youtube. Entonces, básicamente, antes de obtener el video, necesito iniciar sesión para obtener el token de autenticación. Me encuentro con algún tipo de problema con las cookies. Aquí está mi código de registro:CircularRedirectException con autenticación de google
public static String loginUser(String username, String password){
Log.i("Utils", "Start logging user");
// Create a new HttpClient and Post Header
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpPost httppost = new HttpPost("https://www.google.com/youtube/accounts/ClientLogin");
httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
String result = "";
try {
// Add your data
StringEntity tmp =
new StringEntity("Email="+ username +"&Passwd="+ password +"&service=youtube&source=speed bit", "UTF-8");
httppost.setEntity(tmp);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost, localContext);
result = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
finally{
//httpclient.close();
}
Log.i("Utils", "End logging user");
if (result.equals(""))
return result;
String []firstArr = result.split("YouTubeUser");
String []secondArr = firstArr[0].split("=");
result = secondArr[1];
return result;
}
y después de obtener el token de autenticación consigo los videos:
String urlFeed = params[0];
String auth = params[1];
InputStream in;
HttpGet get = new HttpGet(urlFeed);
if (auth != null){
get.setHeader("Authorization","GoogleLogin auth="+auth);
get.setHeader("X-GData-Key",
"key=AI39si5t8eJE_CVcakc-Hzs8wroPcsOHi_fqT03SG6f_lGrJsku0xyfPyB1qZlmYtN5cflfcYAgmvMF2QLy3uG1F6sy-cRzwdg");
}
HttpResponse resp = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
resp = httpClient.execute(get, localContext);
} catch (ClientProtocolException e) {
Log.e(getClass().getSimpleName(), "HTTP protocol error", e);
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Communication error", e);
}
En primer lugar se presenta esta cosa en la Logcat:
08-15 00:21:36.048: WARN/ResponseProcessCookies(4324): Cookie rejected: "BasicClientCookie[version=0,name=PREF,domain=.google.com,path=/,expiry=Wed Aug 14 00:21:36 GMT+02:00 2013]". Illegal domain attribute "google.com". Domain of origin: "www.google.co.il"
y luego se bloquea en resp = httpClient.execute(get, localContext);
Aquí está el logcat para eso:
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): HTTP protocol error
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): org.apache.http.client.ClientProtocolException
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at com.android.mytubedownloader.BaseActivity$YoutubeFeedTask.doInBackground(BaseActivity.java:551)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at com.android.mytubedownloader.BaseActivity$YoutubeFeedTask.doInBackground(BaseActivity.java:1)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at java.lang.Thread.run(Thread.java:1019)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://www.google.co.il/'
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:903)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:468)
08-15 00:21:36.058: ERROR/YoutubeFeedTask(4324): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)