Я пытаюсь отобразить список избранных видео пользователя в YouTube. Поэтому, прежде чем получить видеопоток, мне нужно авторизоваться, чтобы получить токен авторизации. Я столкнулся с какой-то проблемой с файлами cookie. Вот мой код для регистрации:
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;
}
и после получения токена авторизации я получаю видео:
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);
}
Прежде всего он представляет эту вещь в 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"
и затем он падает на resp = httpClient.execute(get, localContext);
Вот логкат для этого:
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)