Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't perform authentication with guess-ga #328

Open
slavoroi opened this issue Feb 7, 2020 · 11 comments
Open

Can't perform authentication with guess-ga #328

slavoroi opened this issue Feb 7, 2020 · 11 comments

Comments

@slavoroi
Copy link
Contributor

slavoroi commented Feb 7, 2020

  1. I created a project in google console.
  2. I created a private key with no restrictions
  3. I opened a google analytics account
  4. I added analytics.js and used angulartics2 for Angular (and the routes recordings worked)
  5. Running :
  plugins: [
    new GuessPlugin({
      debug: true,
      GA: viewId,
      runtime: {
        delegate: false
      },
      routeProvider() {
        return parseRoutes('.');
      }
    })
  ]
};

Can't seem to pass this code in main.js:

 client = google_oauth2_node_1.auth({
                  clientId: clientId,
                  clientSecret: clientSecret,
                  scope: scope
              }).then(function (token) {

Opens a new chrome tab and clicking on my user get's me:

Sign in with Google temporarily disabled for this app
This app has not been verified yet by Google in order to use Google Sign In.

Also when trying:

fetch({
  key,
  viewId,
  period: {
    startDate: new Date('2016-1-1'),
    endDate: new Date('2020-3-3')
  }
}).then(g => {
  writeFileSync('data.json', JSON.stringify(g, null, 2));
}).catch((err) => {
  console.log(err);
  throw err;
});

throws Login Required.
I tried to give guess js read only permissions by email (mgechev@...) in google analytics but it didn't work.

versions:
"guess-parser": "0.4.15",
"guess-webpack": "0.4.15",
"guess-ga": "0.4.14"
Angular 8.2.14

@kasperstorgaard
Copy link
Contributor

kasperstorgaard commented Feb 7, 2020

Dont know about the plugin, but for the second case (using fetch), the documentation is out of date.

What you really need is to generate a JWT auth client like so:

import {fetch} from 'guess-ga';
import {JWT} from 'google-auth-library'

import credentials from './credentials.json';

const auth = new JWT(
  credentials.client_email,
  null,
  credentials.private_key,
  ['https://www.googleapis.com/auth/analytics.readonly']
);

fetch({
  auth,
  viewId,
  period: {
    startDate: new Date('2019-10-01'),
    endDate: new Date('2020-2-01')
  }
})

https://github.com/googleapis/google-auth-library-nodejs#json-web-tokens

edit: changed the scope to analytics.readonly

@slavoroi
Copy link
Contributor Author

slavoroi commented Feb 7, 2020

Yeah! you'r right, except the relevant scope is:
'https://www.googleapis.com/auth/analytics.readonly' and not
'https://www.googleapis.com/auth/cloud-platform'.
Thanks! The fetch works and the Docs should be updated.

*** The DOCS should also mention opening a new service account in google and giving it a view permission for google analytics api (and exporting it to a json) and connecting this email to google analytics api.
The relevant key is not the API key but the key from the JSON which is created from the service account.
*** And using guess plugin with guess ga still doens't work.

@kasperstorgaard
Copy link
Contributor

ah cool, I was getting an issue with that scope too

@mgechev
Copy link
Member

mgechev commented Feb 8, 2020

@kasperstorgaard thanks for pointing this out! Would you have a moment to fix the documentation?

@kasperstorgaard
Copy link
Contributor

@mgechev I'll take a stab at it

@slavoroi
Copy link
Contributor Author

Any update on the issue of using :

module.exports = {
  plugins: [
    new GuessPlugin({
      debug: true,
      GA: viewId,
      runtime: {
        delegate: false
      },
      routeProvider() {
        return parseRoutes('.');
      }
    })
  ]
};

Screenshot from 2020-02-11 12-21-58

@mgechev
Copy link
Member

mgechev commented Feb 11, 2020

@kasperstorgaard
Copy link
Contributor

kasperstorgaard commented Feb 12, 2020

@slavoroi
update, so it turns out the jwt option was not meant for an auth client,
just the credentials.
(it then creates the JWT client on the inside)

const {GuessPlugin} = require('guess-webpack');
const {parseRoutes} = require('guess-parser');
const credentials = require('./credentials.json');
const viewId = '123456789';

module.exports = {
  plugins: [
    new GuessPlugin({
      debug: true,
      jwt: credentials,
      GA: viewId,
      routeProvider() {
        return parseRoutes('.');
      },
      // not sure what this does, I could make it work without it
      runtime: {
        delegate: false
      }
    })
  ]
};

@slavoroi
Copy link
Contributor Author

Yap! it works :)
Works amazing.
Please update your guess-webpack docs and types.

@mgechev
Copy link
Member

mgechev commented Feb 16, 2020

@slavoroi would you open a PR to update what's missing?

@slavoroi
Copy link
Contributor Author

yap 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants