package com.google.sps.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;

public class WorkoutUtils {

  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
    }
  }

  public static JSONObject getJSONObject(
      String workoutLength, String workoutType, String youtubeChannel, int numVideos)
      throws IOException, JSONException {
    String URL = "https://www.googleapis.com/youtube/v3/search?part=snippet";
    String maxResults = "maxResults=" + String.valueOf(numVideos);
    String q = "q=" + String.join("+", workoutLength, workoutType, youtubeChannel);
    String type = "type=video";
    String key = "key=API_KEY_HERE";
    URL = String.join("&", URL, maxResults, q, type, key);
    System.out.println(URL);
    JSONObject json = readJsonFromUrl(URL);
    return json;
  }
}

package com.google.sps.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.json.JSONException;
import org.json.JSONObject;

public class WorkoutUtils {

  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
    }
  }

  public static JSONObject getJSONObject(
      String workoutLength, String workoutType, String youtubeChannel, int numVideos)
      throws IOException, JSONException {
    String apiKey =
        new String(
            Files.readAllBytes(
                Paths.get(WorkoutUtils.class.getResource("/files/youtubeAPIKey.txt").getFile())));

    String URL = "https://www.googleapis.com/youtube/v3/search?part=snippet";
    String maxResults = "maxResults=" + String.valueOf(numVideos);
    String q = "q=" + String.join("+", workoutLength, workoutType, youtubeChannel);
    String type = "type=video";
    String key = "key=" + apiKey;
    URL = String.join("&", URL, maxResults, q, type, key);
    System.out.println(URL);
    JSONObject json = readJsonFromUrl(URL);
    return json;
  }
}

Read YT API key from new file