parro-it / electron-google-oauth

Google api access token in electron
MIT License
56 stars 18 forks source link

Is anyone having issues with the async not receiving a response after authentication? #13

Open bksteckler opened 7 years ago

bksteckler commented 7 years ago

Relatively new to electron so I am not sure if I am missing something in how I structured the code or if there is something amiss.

I have set up a main.js with the included code snippet and I can not seem to get a response from the getAuthorizationCode function to enable the code to continue past the async await call. I am able to get the log in window to appear along with the authentication consent screen. But at that point it redirects to my localhost page but the code is stuck in the async waiting process.

Any suggestions?

const electron = require("electron");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

const path = require("path");
const url = require("url");
const electronGoogleOauth = require("electron-google-oauth");

app.on('ready',createWindow);
function createWindow(){
    const browserWindowParams = {
        'use-content-size': true,
        center: true,
        show: true,
        resizable: true,
        'always-on-top': true,
        'standard-window': true,
        'auto-hide-menu-bar': true,
        'node-integration': false
    };

    const googleOauth = electronGoogleOauth(browserWindowParams);

    (async () => {
        console.log("I am in the async");

        // retrieve  authorization code only 
        const authCode = await googleOauth.getAuthorizationCode(
            ['https://www.google.com/m8/feeds'],
            <<Removed Client Id>>,
            <<Removed Secret Id>>,
            'http://localhost'
        );

        console.log("async await has completed");
        console.log(authCode);

        // retrieve access token and refresh token 
        const result = await googleOauth.getAccessToken(
            ['https://www.google.com/m8/feeds'],
           <<Removed Client Id>>,
            <<Removed Secret Id>>,
            'http://localhost'
        );
        console.log(result);
    })();
}
andrewrt commented 7 years ago

Seeing the same thing here.

I thought this might be due to the contacts API not being enabled in the google developer console, but that doesn't seem to be the issue. (Tried enabling/disabling contacts api, tried switching out the feeds argument for 'profile', etc. But that didn't help).

TimNZ commented 6 years ago

Try this. The BrowserWindow options are out of date, this module was made ages ago.

You only need to call getAccessToken() as it already calls getAuthorizationCode(). You need to hook the app 'window-all-closed' event as the app will by default quit when all windows are closed. Feel free to revert to async, should work fine.

Run in Electron 1.7.7

const electronGoogleOauth = require('electron-google-oauth');
const {app} = require('electron');
const browserWindowParams = {
  center: true,
  show: true,
  resizable: false,
  webPreferences: {
    nodeIntegration: false
  }
};

const clientId = '<clientid>';
const clientSecret = '<clientsecret>';

app.on('window-all-closed',() => {

})
app.on('ready', () => {
  const googleOauth = electronGoogleOauth(browserWindowParams);
  googleOauth.getAccessToken(
    ['https://www.googleapis.com/auth/plus.me',
      'profile',
      'email'],
    clientId,
    clientSecret
  ).then((result) => {
    console.log('result',result);
  })

});
ItsWendell commented 6 years ago

@TimNZ thanks for sharing, works here!

vanseinfo commented 6 years ago

@TimNZ Your code worked!! Thanks for sharing..

vfranco19 commented 2 years ago

Thanks you men