Electron - Error 400 getting geolocation

0

I'm trying to use navigator.geolocation.getCurrentPosition and navigator.geolocation.watchPosition in a PWA project made with Quasar.

It works very well in Browser and Cordova (Android), but an error occurs when publishing on Electron.:

PositionError {
  code: 2, 
  message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 400."
}

Note that it is an Error 400 (Bad Request) and not 403 (Forbidden) , that is, it is not a problem with my API do Google key.

In any case, it follows the excerpt from the file main.js where the API is being configured.:

'use strict'
const
  electron = require('electron'),
  path = require('path'),
  config = require('../config/electron'),
  app = electron.app,
  BrowserWindow = electron.BrowserWindow

let mainWindow
function createWindow () {
  mainWindow = new BrowserWindow({
    ...
  })
  ...
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

process.env.GOOGLE_API_KEY = 'blablabla'
process.env.GOOGLE_DEFAULT_CLIENT_ID = 'blablabla.apps.googleusercontent.com'
process.env.GOOGLE_DEFAULT_CLIENT_SECRET = 'blablabla'
app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

When I call the RestMan direct call, it returns the location without problems.

curl -H "Content-Type: application/json" -X POST -d '{}' https://www.googleapis.com/geolocation/v1/geolocate?key=blablabla
{
    "location": {
        "lat": -x.fffffff,
        "lng": -y.fffffff
    },
    "accuracy": fff
}
    
asked by anonymous 18.01.2018 / 18:48

1 answer

0

The problem occurred when setting variáveis de ambiente to them processo principal (main.js) , they were not being reflected to the rendering process.

My first attempt was to move them to the WebPack build process.

./config/prod.env.js

module.exports = {
  NODE_ENV: '"production"',
  GOOGLE_API_KEY: '"blablabla"',
  GOOGLE_DEFAULT_CLIENT_ID: '"blablabla.apps.googleusercontent.com"',
  GOOGLE_DEFAULT_CLIENT_SECRET: '"blablabla"'
}

./electron/config/webpack.conf.js

var ... config = require('../config/webpack') ...
module.exports = {
  ...
  plugins: [
    new webpack.DefinePlugin({
      'process.env': config.env
    })
    ...
  ]
  ...
}

But the error continued, while trying to access process.env.GOOGLE_API_KEY by DevTools, continued to display the default key adopted by Electron.:

But please identify the following:

if (this.$q.platform.is.electron) {
  let values = process.env
  console.log(process.env)
}

was transpired for.:

var values = Object({
  NODE_ENV: "production",
  GOOGLE_API_KEY: "blablabla",
  GOOGLE_DEFAULT_CLIENT_ID: "blablabla.apps.googleusercontent.com",
  GOOGLE_DEFAULT_CLIENT_SECRET: "blablabla"
});

Then as a contour solution , I needed to set the environment variables.:

if (this.$q.platform.is.electron) {
  let values = process.env
  eval("\
    let electron = require('electron');\n\
    Object.keys(values).forEach(function (key) {\n\
      process.env[key] = electron.remote.process.env[key] = values[key]\n\
    });\n\
  ")
}
    
19.01.2018 / 15:32