Google oauth & apis how to do it

254
July 20, 2017, at 05:30 AM

I can't find any documentation on using google oauth and google apis which I understand. At the moment, I am doing this, but it is not a complete guide: https://developers.google.com/identity/sign-in/web/server-side-flow

What I am trying to do:

I have a node based app, with a simple JS frontend.

  • It needs a google login.
  • When a user logs in, it creates a username for them on my server.
  • It stores some google token somehow under this user on my server.
  • Somehow give a service level access to my backend.
  • This backend can then make changes and read calendar info whenever needed, without user interaction.

At the moment I have a google login which returns an auth code like this: localhost:8000/?code=123. I don't know what to do with that auth code. Documentation is only provided for Java and Python unfortunately.

The next thing is to get the auth tokens (somehow) then figure out how to give my service account access.

Anyone know how?

Answer 1

Have you had a look at PassportJS? It has a very solid implementation for Google

I have done something similar for Facebook and will paste it here in case it helps:

So firstly, I added the below into my node app:

var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;

Then, the below:

app.use(passport.initialize());
app.use(passport.session());

Then, the below:

passport.serializeUser(function(user, done){
    done(null, user.id);
});
passport.deserializeUser(function(id, done){
    userModel.findById(id, function(err, user){
        done(err, user);
    });
});
passport.use(new FacebookStrategy({
clientID: config.facebook.appID,
clientSecret: config.facebook.appSecret,
callbackURL: config.facebook.callbackURL,
profileFields: ['id', 'displayName', 'photos', 'emails', 'birthday', 'location', 'bio', 'likes.limit(100)']
},
function(accessToken, refreshToken, profile, done){
    //Do whatever I want
}
)
);

I won't explain all this but - see if it helps you with your Google apps integration. Good luck!

READ ALSO
How to run bash script from npm script that exports environment variables

How to run bash script from npm script that exports environment variables

I have a packagejson that has the following script:

347
Watch file changes and run eslint using npm

Watch file changes and run eslint using npm

Below is my packagejson contents:

279
How to use mongoose schema when you have dynamic values?

How to use mongoose schema when you have dynamic values?

I am trying to create schema where body can have different keys in it based on the incoming eventSo when i try to rendered data it just send _id to client event is not part of results

254
How to make the react component axios response updated immediately after update/delete?

How to make the react component axios response updated immediately after update/delete?

Hi i am new in using react and axiosI have try the code for simple user CRUD, when i have successfully update the data, then the page will redirected to index of users (using this

331