0

I am trying to use the Youtube GDATA API in order to add a new playlist to a youtube account. I base my code on the documentation: https://developers.google.com/youtube/2.0/developers_guide_protocol_playlists#Adding_a_playlist

I first get an access token and use my developer key appropriately. The post seems to work just fine, but when trying to get back the response, I get a file not found exception while calling getInputStream.

Does anyone has an idea? Thanks

Here is the connection code (an updated cleaner version):

@Override
protected Boolean doInBackground(Void... arg0) {
    BufferedReader input = null;
    InputStreamReader inputStreamReader = null;
    StringBuilder postContentXml = new StringBuilder();

    postContentXml.append("<?xml version='1.0' encoding='UTF-8'?>").
        append("<entry xmlns='http://www.w3.org/2005/Atom'") .
        append(" xmlns:yt='http://gdata.youtube.com/schemas/2007'>").
        append("<title type='text'>Sports Highlights Playlist</title>").
        append("<summary>A selection of sports highlights</summary>").
        append("</entry>");

    byte[] buffer = postContentXml.toString().getBytes();
    StringBuilder response = new StringBuilder();

    try {
        URL url = new URL("https://gdata.youtube.com/feeds/api/users/default/playlists");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        // Initialize connection parameters
        urlConnection.setConnectTimeout(30000);
        urlConnection.setReadTimeout(30000);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");

        // Headers initialization
        urlConnection.setRequestProperty("Content-Type", "application/atom+xml");
        urlConnection.setRequestProperty("Content-Length", String.valueOf(buffer.length));
        urlConnection.setRequestProperty("Authorization", mAuthToken);
        urlConnection.setRequestProperty("GData-Version", "2");
        urlConnection.setRequestProperty("X-GData-Key", YoutubeUtils.getDevKey());

        OutputStream os = urlConnection.getOutputStream();
        os.write(buffer, 0, buffer.length);
        os.flush();
        os.close();

        InputStream inputStream = urlConnection.getInputStream();

        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");


        input = new BufferedReader(inputStreamReader, 4096);

        String strLine = null;
        while ((strLine = input.readLine()) != null) {
            response.append(strLine);
        }

        input.close();
        inputStreamReader.close();
        inputStream.close();
        urlConnection.disconnect();

        Log.d("CreatePlaylistTask", "Response: " + response);
    }
    catch(Exception e) {
        Log.d("CreatePlaylistTask", "Error occured: " + e.getMessage());
    }

    return true;
}
asaf
  • 123
  • 1
  • 2
  • 8

2 Answers2

2

I'm assuming that the POST wasn't actually successful.

If I had to guess from looking at your code, I'd think that the problem might be the Authorization header value. What does myAuthToken look like, and what type of token is it? If it's an OAuth 2 token, for instance, then the value needs to be Bearer TOKEN_VALUE, not just TOKEN_VALUE.

Also, please note that v3 of the YouTube Data API will be released in the near future, and it will offer better support on Android using the new Google APIs Client Library for Java.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • And ofcourse, you are right :) Thank you Jeff for the quick response. The POST failed with a 403 response. I've didn't pay attention that this API may only returns a status result, and not a full XML or JSON response. I get the token using Android's AccountManager getAuthToken function, including re-validating the token, but with no success. I've included the bearer but it didn't work. Eventually, I did come across the Google APIs Client library, as you've stated, including a nice Google IO video from the latest March. I will try to use it shortly. – asaf Oct 25 '12 at 18:35
  • BTW, if you've mentioned a near release of the data API, can you please elaborate about the state of the Android native embed API, which was introduced on June. I can't seem to find any timeframe reference anywhere on the web. I do not want to use illegal direct stream embedding, and working with the IFrame embed API has many flaws and bugs :( – asaf Oct 25 '12 at 18:36
  • The Data API will be public very soon. It will take somewhat longer for the native Android YouTube Player API to be released, but not an inordinately long period of time. (Sorry that I can't get more specific.) – Jeff Posnick Oct 25 '12 at 21:30
0

I have put together a sample Android application which uses the YouTube Data v3 API to demonstrate how you can load a playlist into a ListView.

https://github.com/akoscz/YouTubePlaylist

Note that you MUST have a valid API key for this sample application to work. You can register your application with the Google Developer Console and enable the YouTube Data API. You need to Register a Web Application NOT an Android application, because the API key that this sample app uses is the "Browser Key".

Akos Cz
  • 12,711
  • 1
  • 37
  • 32