Sunday, 22 September 2019

'Login Required' error. How to login after getting oauth2 token with Google API

I'm building a small Node/React app where I'm trying to implement OAuth2 Google to request the Google Analytics API. I'm using Passport.js to handle the authentification.

I'm able to get the Token correctly:

router.get(
    "/auth/google/callback",
    passport.authenticate("google", { failureRedirect: "/error", session: false }),
    function(req, res) {
        var token = req.user.token;
        res.redirect("http://localhost:5000/?token=" + token);
    }
);

However, when trying to call the Google Analytics management API I have the following error Error: Login Required.

router.get('/getData', function(req, res) {
    googleAccounts.management.profiles.list(
      {
        accountId: '~all',
        webPropertyId: '~all'
      },
      (err, data) => {
        if (err) {
          console.error('Error: ' + err)
          res.send('An error occurred')
        } else if (data) {

          Console.log(data)

        }
      }
    )
})

How do I login? What step I'm missing?



from 'Login Required' error. How to login after getting oauth2 token with Google API

No comments:

Post a Comment