5

I'm trying to display a user's favorites videos list in youtube. So basically before getting the video feed I need to login to get the auth token. I am encountering some sort of a problem with cookies. Here is my code for logging:

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;
     }

and after I get the auth token I get the 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);
                    }

First of all it presents this thing in the 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"

and then it crashes on resp = httpClient.execute(get, localContext); Here is the logcat for that:

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)

3 Answers 3

0

whenever I get an HTTP protocol error it was because of security. You could try HttpPost httppost = new HttpPost("**http**://www.google.com/youtube/accounts/ClientLogin");

and remove the s from https. That usually makes my problems go away.

Good Luck!

0

I'm not sure if this is the whole reason, but this document says that URL is deprecated. Hope that helps somehow.

This URL was updated in October 2010. The old URL has been deprecated in accordance with the policy explained in our Terms of Service. If you are updating an application that used the old URL (https://www.google.com/youtube/accounts/ClientLogin), note that the new URL does not return the user's YouTube account name as the YouTubeUser value in response to a successful ClientLogin request.

0

You should not access Youtube through website URLs. The official way to access videos is Youtube Data API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.