2
votes

I just moved my first mean app to aws and considering all things went pretty well. But my question is now that I have it up and running my google calendar api is not working. I checked the logs and noticed since I changed URL the api wanted to revalidate by visiting

Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&response_type=code&client_id=628160639508-njce0d3437e19gs8t4gn5at3surr3seu.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob Enter the code from that page here:

Now from the logs I can easily grab the url and validate but then it gives a code and directs back the app to enter the code for validation. How can I do that when its running on aws elastic beanstalk?

Any help is greatly appreciated. Thanks

2

2 Answers

0
votes

As mentionned in this documentation: https://developers.google.com/calendar/quickstart/nodejs, you need to get a new token with this new code.

Here is a sample that might help you:

/**
 * Get and store new token after prompting for user authorization, and then
 * execute the given callback with the authorized OAuth2 client.
 * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
 * @param {getEventsCallback} callback The callback for the authorized client.
 */
function getAccessToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES,
  });
  console.log('Authorize this app by visiting this url:', authUrl);
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });
  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();
    oAuth2Client.getToken(code, (err, token) => {
      if (err) return callback(err);
      oAuth2Client.setCredentials(token);
      // Store the token to disk for later program executions
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) console.error(err);
        console.log('Token stored to', TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}
0
votes

According to my understanding, you have registered a redirect URL in the calendar API and once you start the validation process after the login page the control flow redirects to that URL with the all the credentials (tokens).

Here you can create an endpoint in the node application for example /token and register it in the calendar API. Once the redirection happens you can save the data inside a map in node application which can be used later on for all other calendar API calls.

Here is a small example of the token endpoint:

var google        = require("googleapis");

var OAuth2        = google.auth.OAuth2;

var sessMap       = require('./sessionMap');

var oauth2Client  = new OAuth2(<client_id>, <access_token>, <domain>+"/token");

var app           = require('express')();

app.all('/token', function(req, res){
   var code = req.query.code;
   oauth2Client.getToken(code, function(err, tokens) {
    if (err) {
      res.send(err);
      return;
    }
    oauth2Client.setCredentials(tokens);

    //updated after sessionMap
    sessMap.set('googleAuth', tokens);        

    res.send("This page will now redirect you");

   });         
});

To save credentials you can create a session map as shown below:

sessionMap.js

var hashmapSession = {};
exports.sess = {
  set : function(key, value){
    hashmapSession[key] = value;
  },
  get : function(key){
    return hashmapSession[key];
  },
  all : function(){
    return hashmapSession;
  },
  delete : function(key){
    delete hashmapSession[key];
  }
};

The above code will have to be incorporated in your node application.