/**
 * Sample Java code for youtube.videos.list
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/guides/code_samples#java
 */
package com.google.musicanalysis.site;
import com.google.musicanalysis.util.Secrets;
import com.google.musicanalysis.util.URLEncodedBuilder;


import com.google.gson.JsonParser;
import com.google.musicanalysis.util.URLEncodedBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;


@WebServlet("/api/youtube")
public class YoutubeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
        // don't know how to catch null pointer exception

        // retrieve Youtube API key and access token 
        String API_KEY = Secrets.getSecretString("YOUTUBE_API_KEY");
        HttpSession session = req.getSession();
        String accessToken = session.getAttribute("youtube_access_token").toString();
    
        // make http request to youtube API
        var youtubeParam = new URLEncodedBuilder()
            .add("part", "topicDetails")
            .add("myRating", "like")
            .add("key", API_KEY);
        URI youtubeUri = URI.create("https://www.googleapis.com/youtube/v3/videos?" + youtubeParam.build());

        var httpClient = HttpClient.newHttpClient();
        var youtubeReq = HttpRequest.newBuilder(youtubeUri)
                .header("Authorization", "Bearer " + accessToken)
                .header("Accept", "application/json")
                .GET()
                .build();

        // get response to Youtube API 
        var youtubeRes = httpClient.sendAsync(youtubeReq, BodyHandlers.ofString()).join();
        var youtubeResBody = youtubeRes.body();
        res.getWriter().write(youtubeResBody);
    }
}

@@ -1110,109 +1110,149 @@
  // 
-don't know how to catch null pointer exception%0A%0A        // retrieve Youtube API key and
+retrieve Youtube API key and access token %0A        String API_KEY = %22%22; %0A        HttpSession session = null;%0A        String
  access
- t
+T
 oken 
+= %22%22;
 %0A   
@@ -1248,38 +1248,49 @@
 n = %22%22;%0A        
-S
 tr
-ing
+y %7B%0A           
  API_KEY = Secre
@@ -1332,35 +1332,27 @@
 %22);%0A        
-HttpSession
+   
  session = r
@@ -1368,38 +1368,35 @@
 sion();%0A        
-String
+   
  accessToken = s
@@ -1447,24 +1447,129 @@
 toString();%0A
+        %7D catch (NullPointerException e) %7B%0A            res.setStatus(401);%0A            return;%0A        %7D%0A
     %0A       

throw 401 error for nullpointer exception