Coder Social home page Coder Social logo

Comments (8)

gearsdigital avatar gearsdigital commented on May 9, 2024

Found a workaround using https://github.com/tj/co

  getToken(code) {
    let oauth2Client = this.oauth2Client;
    oauth2Client.getToken(code, function (err, tokens) {
      if (!err) {
        co(function*() {
          oauth2Client.setCredentials(tokens);
          yield Database.from('admin').insert({'access_token': tokens.access_token});
          return tokens.access_token;
        }).then(function (token) {
          console.info(`Token saved to database (${token}).`);
        }, function (err) {
          console.error(err.stack);
        });

      }
    });
  }

from core.

thetutlage avatar thetutlage commented on May 9, 2024

how are you calling getToken in your controller ?

from core.

gearsdigital avatar gearsdigital commented on May 9, 2024

This is the controller:

'use strict';
const GoogleCalendarService = use('App/Services/GoogleCalendarService');

class CalendarController {

  constructor() {
    this.calendarService = new GoogleCalendarService();
  }

  * authorize(request, response) {
    let params = request.get();
    this.calendarService.getToken(params.code);

    const view = yield response.view('index.html');
    response.ok(view);
  }

  * index(request, response) {
    const view = yield response.view('index.html', {url: this.calendarService.generateAuthenticationUri()});
    response.ok(view);
  }

}

module.exports = CalendarController;

And here the appropriate route:

Route.get('/auth', 'CalendarController.authorize');

from core.

thetutlage avatar thetutlage commented on May 9, 2024

You will have to use the yield while using getToken since it is a generator method

yield this.calendarService.getToken(params.code);

from core.

gearsdigital avatar gearsdigital commented on May 9, 2024

I tried it already, but it did not work out. In this case i got the parse error:

yield Database.from('admin').insert({'access_token': tokens.access_token}); ^ ParseError: Unexpected token

screen

getToken(code) {
    let oauth2Client = this.oauth2Client;
    oauth2Client.getToken(code, function (err, tokens) {
      if (!err) {
        oauth2Client.setCredentials(tokens);
        yield Database.from('admin').insert({'access_token': tokens.access_token});
      }
    });
}
* authorize(request, response) {
    let params = request.get();
    yield this.calendarService.getToken(params.code);

    const view = yield response.view('index.html');
    response.ok(view);
}

from core.

thetutlage avatar thetutlage commented on May 9, 2024

Ohh got it, coz you are trying to run it inside the callback of oauth2Client.getToken and generators needs to be at the first level of * method.

I am not a big fan of callbacks and if you are interested you can make use of https://github.com/tj/node-thunkify. It will convert your callbacks to generator friendly methods. Something like this

const oauthToken = thunkify(oauth2Client.getToken)
try {
  const tokens = yield oauthToken(code)
  oauth2Client.setCredentials(tokens);
  console.log(`Save ${tokens.access_token} to database`);
  yield Database.from('admin').insert({'access_token': tokens.access_token});
}
catch (e) {
  console.log(e)
}

from core.

gearsdigital avatar gearsdigital commented on May 9, 2024

I'll try that... :) Thank you for your fast and helpful support bro! 👍

from core.

lock avatar lock commented on May 9, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.