Youtube Get uploaded video list. FileNotFound Error
Asked Answered
C

3

6

I am trying to get list of videos uploaded on my YouTube channel using following

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId{MY_CHANNEL_ID}&maxResults=50&key={MY_APP_ID}

I have created App in Google App Console and generate APP ID for the same. But when I trying to access it through my Android application getting java.io.FileNotFoundException error

I have given application identifier and SHA1 also, If I try to access through Web Browser key without any other constrains it works well and returns all the video list but in case of Android it is not working.

@Override
    protected String doInBackground(Void... voids) {

        HttpURLConnection urlConnection = null;

        String urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrF362wkcVnjqqPRsSEzvgg&maxResults=50&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8";
        //urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLTqSMwQKOhUhl7gm7h6YwX6XPYr0ViBtu&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8";

        String jsonString = new String();

        URL url = null;
        try {
            url = new URL(urlString);

        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);

        urlConnection.setDoOutput(true);

        urlConnection.connect();

        BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));

        char[] buffer = new char[1024];

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line+"\n");
        }
        br.close();

        jsonString = sb.toString();

        System.out.println("JSON: " + jsonString);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonString;
    }

Error Log

BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrF362wkcVnjqqPRsSEzvgg&maxResults=50&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8
05-13 15:28:26.607 19118-19151/com.jeevanmulmantra W/System.err: java.io.FileNotFoundException: https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCrF362wkcVnjqqPRsSEzvgg&maxResults=50&key=AIzaSyAiAFjZb1eVdRxVWnymrhuAb1iDlmYupu8

enter image description here

Constitutionalism answered 13/5, 2017 at 7:37 Comment(7)
Please post the code.Vomit
it seems that your request is incomplete, so the api returns 403. Try correcting thatCoppins
@Coppins Any Idea what's missing??Constitutionalism
i'm not familiar with youtube api, sorryCoppins
Post the screen from your API Console that has the Key Restrictions radio buttons.Vomit
Just for confirming. I am not sure if you are using production certificate or debug, so did you try adding both? and if possible please state the method you used to get the SHA-1 for both of them.Malonylurea
@Malonylurea For now I tried only Debug certificate. To get SHA-1 I am using standard command line tool with following command "keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android"Constitutionalism
V
3

Without looking at your API Console settings, I cannot say for sure. But from looking at the HTTP response, it looks like your IP address might be blocked by the Google authorization server. Hence, it is sending you back an unauthorized request HTTP status code 403.

Go to your API Console and select the None radio button from under the Key restrictions section. Then try again. It should work.

Vomit answered 13/5, 2017 at 10:38 Comment(2)
Yes, Without None Key restrictions it is working fine as I mentioned, But When I am trying to add Key restriction for Android using Package name and SHA1 it is not working. I have attached Console screenshot as well. Please checkConstitutionalism
Did you get any idea??Constitutionalism
W
0

This is not exactly what you are asking but I will share my experience.

I had a similar situation a couple of months ago. Seaching on line I got to the conclusion that the YouTube api option for Android just doesn't work. I ended up with a more convenient solution for my development:

I got a YT api key for a website and bound it to the corresponding domain.

I Created a php file that gets the playlist from youtube twice a day using curl and cron job setup on the server. The playlist file in json format is then written to the web server (refreshed twice a day).

The Android app connects to my server intead of YTs and get the "cached" json play list from there.

This option drastically reduces the impact on the quota consumption because all the hits to the playlist go to the website.

If you are interested in this option I can share the php used for getting the play list.

Weaner answered 16/5, 2017 at 1:47 Comment(1)
This is really a good work around, I will love to follow this approach if not find any solution for this issue. Will let you know If I needed php. Thanks for your inputConstitutionalism
P
0

I had the same problem, just regenerate another key and restrict it again with the package name and SHA1 from your Android Studio. It wasn't working with the old key regenerating the key and restricting it again worked for me.

Pelisse answered 14/7, 2018 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.