Electron - Error 400 when obtaining geolocation

Asked

Viewed 61 times

0

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

It is working very well in Browser and Cordova (Android), but an error occurs when publishing in 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), I mean, it’s not a problem with my key to the API do Google.

In any case, follow 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 make the direct call from Restman, he returns the location smoothly.:

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
}
  • Not I’m sure, but I think it’s a bug in version 58.0.3029.110 of Chromium (currently the version that Electron uses), this bug was fixed I believe in version 61, so only when they update Chromium to a newer version than 61 to fix the problem.

1 answer

0


The problem occurred when setting the variáveis de ambiente, to them processo principal (main.js), they were not being reflected into 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
    })
    ...
  ]
  ...
}

However, the error continued when trying to access the process.env.GOOGLE_API_KEY by Devtools, continued to display the standard key adopted by Electron.:

but identify that the following condition.:

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

was perspirated to.:

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

Then how contouring 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\
  ")
}
  • @Guilhermenascimento now I must find a way to get rid of this gambiarra.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.