0

I am trying to add a simple table of youtube videos from a public playlist to my existing Android app by using the YouTube Data API PlaylistItems: list call.

I have been successful in getting it to work on the iOS version of my app, but it is not working with on the Android version of my app. I do not want to (and shouldn't have to) use OAuth, but only have to use my generated API key. I've tried using an Android API key as well as a Browser API key, but neither of them are working.

I keep getting this error message:

"error": {  
    "errors": [   {    
        "domain": "global",    
        "reason": "required",    
        "message": "Login Required",    
        "locationType": "header",    
        "location": "Authorization"   }  ],  
    "code": 401,  
    "message": "Login Required" }}

This is my URL: https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=MY_PLAYLIST_ID&key=MY_API_KEY This URL works for the iOS version,when I type it into the browser, and here as well: https://developers.google.com/youtube/v3/docs/playlistItems/list#examples, all of which are not using any type of authentication. I've tried renewing my API key, but that hasn't worked either. Here is my code, in case it helps:

@Override
        protected String doInBackground(Void... params) {
            try {
                URL urlConnection = new URL(url_from_above);
                HttpURLConnection connection = (HttpURLConnection) urlConnection
                        .openConnection();
                connection.setReadTimeout(15000);
                connection.setConnectTimeout(15000);
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestMethod("GET");

                int responseCode = connection.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    String line;
                    String jsonString = "";
                    BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    while ((line=br.readLine()) != null) {
                        jsonString+=line;
                    }
                    result = jsonString;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
Laurel
  • 5,965
  • 14
  • 31
  • 57
erics
  • 111
  • 11

1 Answers1

0

So after some more long trial and error I discovered that the problem was with connection.setDoOutput(true); After some brief research as to why that would break it, I found this: What exactly does URLConnection.setDoOutput() affect?

Community
  • 1
  • 1
erics
  • 111
  • 11