Saturday 20 May 2017

My express web application cannot authenticate with Google APIs

I am working with the Typescript code below and I have configured everything as simply as possible. On console.developer.google.com I have enabled: Google+ API, Admin SDK, and the Groups Settings API. However, when I try to make a GET request to https://www.googleapis.com/groups/v1/groups/address@domain.tld I always receive 401: Login Required.

"use strict";

// Module dependencies
import * as bodyParser from "body-parser";
import * as express from "express";
import * as google from "googleapis";
import * as GoogleStrategy from "passport-google-oauth20";
import * as logger from "morgan";
import * as passport from "passport";
import * as path from "path";
import * as session from "express-session";

namespace Groups {
    class Express {
        public app = express();

        constructor(port) {
            // Configure view engine
            this.app.set("views", path.join(__dirname, "views"));
            this.app.set("view engine", "ejs");

            // Configure morgan
            this.app.use(logger("dev"));

            // Configure bodyParser
            this.app.use(bodyParser.json());
            this.app.use(bodyParser.urlencoded({ extended: false }));

            // Configure static content delivery
            this.app.use(express.static(path.join(__dirname, "public")));

            // Configure sessions
            this.app.use(session({
                secret: "secret",
                resave: true,
                saveUninitialized: true,
            }));

            // Configure the Google strategy for use by Passport.js.
            //
            // OAuth 2-based strategies require a `verify` function which receives the
            // credential (`accessToken`) for accessing the Google API on the user's behalf,
            // along with the user's profile. The function must invoke `cb` with a user
            // object, which will be set at `req.user` in route handlers after
            // authentication.
            passport.use(new GoogleStrategy({
                clientID: "",
                clientSecret: "",
                callbackURL: "http://localhost:3000/auth/google/callback",
            }, (accessToken, refreshToken, profile, callback) => {
                // Extract the minimal profile information we need from the profile object
                // provided by Google
                callback(null, profile);
            }));

            // Initialize Passport and restore authentication state, if any, from the
            // session.
            this.app.use(passport.initialize());
            this.app.use(passport.session());

            // Configure routes
            this.app.get("/auth/google",
                // Start OAuth 2 flow using Passport.js
                passport.authenticate("google", { scope: [ "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/apps.groups.settings" ] })
            );

            this.app.get("/auth/google/callback", passport.authenticate('google', { failureRedirect: '/' }),
                function(request, response) {
                    // Finish OAuth 2 flow using Passport.js
                    response.redirect("/");
                }
            );

            this.app.get("/auth/logout", function(request, response) {
                //request.logout();
                response.redirect("/");
            });

            this.app.get("/", function(request, response) {
                response.render("index");
            })

            // Listen on provided port
            this.app.listen(port, function() {
                console.log("Listening on port", this.address().port);
            });
        }
    }

    new Express(3000);
}

I am trying to recreate this web application in an effort to teach myself Restful APIs.

Any help would be greatly appreciated.



via Brian Jenkins

No comments:

Post a Comment