Unable to authenticate with Google Tasks - Homework

132 Views Asked by At

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:

  • Obtain and show issues from public organizations from GitHub
  • Obtain authentication thru OpenID Connect(OAuth 2.0)
  • Create a Google Task on the default tasklist from an issue using REST

Note: I can only use HTTP, no jar libs


The first part was easy, just had to make the request to the GitHub API and parse the JSON, no problem here


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:

Login.java

...
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);
}
...

callback.java

...
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");
}
...

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

Note: To get the API Key I used the Google Developer Console and created a key for Browsers

GoogleStuff.java

...
public static String AUTH_LINK = "https://accounts.google.com/o/oauth2/auth?"+
                                "scope=https://www.googleapis.com/auth/tasks&"+
                                "redirect_uri=http://localhost:5005/callback&"+
                                "response_type=code&" +
                                "client_id=" + FirstHttpServer.CLIENT_ID +
                                "&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();
    }

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 :)

Thanks in advance

1

There are 1 best solutions below

0
On

An API will indicate that an access token has expired when it returns a 401 status code.To obtain a new access token, make a request to the token endpoint and include the client_id, client_secret, refresh_token, and grant_type parameters.

You can find more information in this link.

Hope that helps!