title: Programmatically accessing a Google App Engine Standard servlet that requires Google login
body: <p>The following is a simple Google App Engine Standard servlet that displays a user’s Google e-mail address if the user is logged in.  How can I invoke this programmatically (e.g. using curl or Java code) while providing Google credentials (e.g. for a user or service account).  I think I need to obtain an OAuth2 token, but I could use some help coming up with a step-by-step process.</p>
  
  <pre><code>package com.example.appengine.java8;
  
  import java.io.IOException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.PrintWriter;
  import com.google.appengine.api.users.User;
  import com.google.appengine.api.users.UserService;
  import com.google.appengine.api.users.UserServiceFactory;
  
  @WebServlet(name = "HelloAppEngine", value = "/hello")
  public class HelloAppEngine extends HttpServlet {
  
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  
      UserService userService = UserServiceFactory.getUserService();
      User user = userService.getCurrentUser();
      response.setContentType("text/plain");
      PrintWriter out = response.getWriter();
      if(user == null) {
        out.print("not authenticated");
      }
      else {
        out.print(user.getEmail());
      }
  
    }
  
  }
  </code></pre>
  
  <p>Here is a live version of this servlet:</p>
  
  <p><a href="https://servlet-authentication-test.appspot.com/hello" rel="nofollow noreferrer">https://servlet-authentication-test.appspot.com/hello</a></p>
  
  <p>You can use the following link to login with a Google account and then access the servlet while authenticated:</p>
  
  <p><a href="https://accounts.google.com/signin/v2/identifier?service=ah&amp;passive=true&amp;continue=https%3A%2F%2Fappengine.google.com%2F_ah%2Fconflogin%3Fcontinue%3Dhttps%3A%2F%2Fservlet-authentication-test.appspot.com%2Fhello&amp;flowName=GlifWebSignIn&amp;flowEntry=ServiceLogin" rel="nofollow noreferrer">https://accounts.google.com/signin/v2/identifier?service=ah&amp;passive=true&amp;continue=https%3A%2F%2Fappengine.google.com%2F_ah%2Fconflogin%3Fcontinue%3Dhttps%3A%2F%2Fservlet-authentication-test.appspot.com%2Fhello&amp;flowName=GlifWebSignIn&amp;flowEntry=ServiceLogin</a></p>