title: Unable to authenticate with Google Tasks - Homework
body: <p>This week I had a small program to develop, I needed to create a Web Application(using a Java Servlet on localhost), this Web App is required to do the following:</p>
  
  <ul>
  <li>Obtain and show issues from public organizations from GitHub</li>
  <li>Obtain authentication thru OpenID Connect(OAuth 2.0)</li>
  <li>Create a Google Task on the default tasklist from an issue using REST</li>
  </ul>
  
  <p>Note: I can only use HTTP, no jar libs</p>
  
  <hr>
  
  <p>The first part was easy, just had to make the request to the GitHub API and parse the JSON, no problem here</p>
  
  <hr>
  
  <p>The second part was somewhat easy, I had to create a new Client ID in Google Developer Console, where I'd set the callback and receive the code on it, I'll put it here just in case I'm doing something wrong with it:</p>
  
  <p><strong>Login.java</strong></p>
  
  <pre><code>...
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      System.out.println("--New login request was received --");        
      resp.setStatus(302); 
      resp.setHeader("Location", GoogleStuff.AUTH_LINK);
  }
  ...
  </code></pre>
  
  <p><strong>callback.java</strong></p>
  
  <pre><code>...
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      Cookie c = new Cookie("googleCode", req.getParameter("code")); c.setMaxAge(60*60); //1 Hour
      //Example code received from Google
      //4/6POIUYwZA3tFCnX_2feRDGiPMQOU7At8HyfOzemMkOY.wtiPpsElo8wZoiIBeR5Q2m9sqEaFkwI
      resp.addCookie(c);
      resp.setStatus(302);
      resp.setHeader("Location","searchOrg");
  }
  ...
  </code></pre>
  
  <hr>
  
  <p>My problem comes on the third part, I get the response code 401(Not Authorized) from Google, I'm sure I'm doing something wrong, but I don't really know what is wrong. This is probably all wrong, so bear with it :p</p>
  
  <p>Note: To get the API Key I used the Google Developer Console and created a key for Browsers</p>
  
  <p><strong>GoogleStuff.java</strong></p>
  
  <pre><code>...
  public static String AUTH_LINK = "https://accounts.google.com/o/oauth2/auth?"+
                                  "scope=https://www.googleapis.com/auth/tasks&amp;"+
                                  "redirect_uri=http://localhost:5005/callback&amp;"+
                                  "response_type=code&amp;" +
                                  "client_id=" + FirstHttpServer.CLIENT_ID +
                                  "&amp;approval_prompt=force";
  ...
  public static void addTask(Issue i, String googleCode){
      try {
          String postURL = "https://www.googleapis.com/tasks/v1/lists/%40default/tasks?key=" + MyServer.API_KEY;
          URL url = new URL(postURL);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setDoOutput(true);
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "application/json");
          connection.setRequestProperty("Authorization", googleCode);
          BufferedWriter httpRequestBodyWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
          httpRequestBodyWriter.write(i.toJson());
          httpRequestBodyWriter.close();
          Scanner httpResponseScanner = new Scanner(connection.getInputStream());
          while(httpResponseScanner.hasNextLine())
              System.out.println(httpResponseScanner.nextLine());
          httpResponseScanner.close();
      } catch (IOException e) {
          System.out.println(e.toString());
          throw new RuntimeException();
      }
  </code></pre>
  
  <p>I've been at it for a couple days, but with other projects also tightening my time, its becoming increasingly hard for me to find the problem with this, which is why I request your help :)</p>
  
  <p>Thanks in advance</p>