// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package servlets;

import com.google.gson.JsonParser;
import java.io.IOException;
import java.io.FileInputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import javax.servlet.annotation.WebServlet;
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 com.google.gson.JsonObject;
import com.google.gson.JsonElement;

/** A servlet to request access and refresh tokens. */
@WebServlet("/api/oauth/callback/sheets")
public class OAuthCallbackServlet extends HttpServlet {
  // Token request parameters.
  private final String CLIENT_ID = "client_id=150737768611-svndjtlklolq53g4ass4r3sqal2i31p5.apps.googleusercontent.com";
  private final String GRANT_TYPE = "grant_type=authorization_code";
  private final String CODE = "https://accounts.google.com/o/oauth2/v2/auth";
  private final String TOKEN_URI = "https://oauth2.googleapis.com/token";

  // Figure out a way to secure method to retrieve the client secret.
  private final String CLIENT_SECRET = "";
  private final String REDIRECT_URI = "redirect_uri= SOME CALLBACK URI";


  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String error = request.getParameter("error");
    String code = request.getParameter("code");

    // Print any error the OAuth provider gave us.
    if (error != null && !error.isEmpty()) {
      response.getWriter().print(error);
      response.setStatus(401);
      return;
    }

    // OAuth provider should have given us a code, otherwise something is wrong here
    if (code == null || code.isEmpty()) {
      response.setStatus(400);
      return;
    }

    String tokenRequestBody = String.format("%s?%s&%s&%s&%s", GRANT_TYPE, CODE,
      REDIRECT_URI, CLIENT_ID, CLIENT_SECRET);

    // Request the tokens.
    HttpClient httpClient = HttpClient.newHttpClient();
    HttpRequest tokenRequest = HttpRequest.newBuilder(URI.create(TOKEN_URI))
      .header("Content-Type", "application/x-www-form-urlencoded")
      .POST(BodyPublishers.ofString(tokenRequestBody)).build();

    HttpResponse tokenResponse = httpClient.sendAsync(tokenRequest, BodyHandlers.ofString()).join();
    String tokenResponseBody = tokenResponse.body().toString();

    // Parses to find access token.
    JsonObject tokenResponseObj = JsonParser.parseString(tokenResponseBody).getAsJsonObject();
    JsonElement accessToken = tokenResponseObj.get("access_token");

    response.setContentType("text/html");
    response.getWriter().printf("<h1>the access token for the Sheets API is %s</h1>", accessToken);
  }
}
@@ -650,27 +650,17 @@
 java.io.
-IOException
+*
 ;%0Aimport
@@ -669,26 +669,14 @@
 ava.
-io.FileInputStream
+util.*
 ;%0Aim
@@ -1748,79 +1748,8 @@
 en%22;
-%0A%0A  // Figure out a way to secure method to retrieve the client secret.
 %0A  p
@@ -1785,16 +1785,30 @@
 CRET = %22
+client_secret=
 %22;%0A  pri
@@ -1875,16 +1875,96 @@
 K URI%22;%0A
+  private final String SECRET_FILEPATH = %22../../src/main/resources/secret.txt%22;%0A
 %0A%0A  @Ove
@@ -2659,24 +2659,71 @@
 T_SECRET);%0A%0A
+%0A%0A    System.out.println(getClientSecret());%0A%0A%0A
     // Reque
@@ -3513,10 +3513,14 @@
 en);%0A  %7D
+%0A%0A  
 %0A%7D

create framework of OAuthCallbackServlet