paulirish / pwmetrics

Progressive web metrics at your fingertipz
Apache License 2.0
1.25k stars 74 forks source link

Cant seem to get auto google sheets upload to work #167

Closed jooj123 closed 6 years ago

jooj123 commented 6 years ago

Pretty sure this is a bug

In the google sheets setup here: https://github.com/paulirish/pwmetrics#defining-submit

It says "Take a client_secret and put it into the config as value of clientSecret property." - not sure what this means exactly. I have downloaded the JSON file from google sheets api setup - thats in the format:

{
  "type": "service_account",
  "project_id": "blah",
  "private_key_id": "blah",
  "private_key": "keyblah",
  "client_email": "blah",
  "client_id": "blah",
  "auth_uri": "blah",
  "token_uri": "blah",
  "auth_provider_x509_cert_url": "blah",
  "client_x509_cert_url": "blah"
}

Does it mean just grabbing this object and putting it into pwmetrics-config.js clientSecret property ?

I have tried this with example config here:

...
  sheets: {
    type: 'GOOGLE_SHEETS', 
    options: {
      spreadsheetId: 'myspreadheetid',
      tableName: 'data',
    },
  },
  clientSecret: {
    // the object above
  },

But this doesnt work - I get error:

TypeError: Cannot read property 'client_secret' of undefined
    at GoogleOauth.<anonymous> (/Users/martin.jujou/Documents/projects/fe-server-search-results/node_modules/pwmetrics/lib/oauth/google-oauth.js:51:56)

Seems that the code here: https://github.com/paulirish/pwmetrics/blob/1f70ee3137a157186de4ef0b56f4bd8c94524a3e/lib/oauth/google-oauth.ts#L45

Seems incorrect as it checks for an "installed" property ? I can fix this up but not sure if this property is there for a reason?

Also seems you need to configure a redirect uri in order for oauth. Is there any way to setup with a service account so that this manual step is not required? I would like for this sheets api integration to be part of CI - I can see there is an example to integrate puppeteer to do the login but this seems a bit clunky: https://github.com/google/google-auth-library-nodejs/pull/265

denar90 commented 6 years ago

Hi. Looks like you generated wrong client secret. Format of secret key has to be like this https://github.com/paulirish/pwmetrics/blob/v3.2.0/types/types.ts#L90

Does it mean just grabbing this object and putting it into pwmetrics-config.js clientSecret property ?

Yes it is)

Here's format of mine

clientSecret:
{
   "installed": {
      "client_id": "id",
      "project_id": "pwmetrcis",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://accounts.google.com/o/oauth2/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_secret": "key",
      "redirect_uris": [
         "url",
         "http://localhost"
      ]
   }
}
jooj123 commented 6 years ago

@denar90 this format works

Seems its not clear from the docs so thats where the confusion lies - Ill throw up a PR

denar90 commented 6 years ago

Cool 👍 I'd like to merge your PR ;)

jooj123 commented 6 years ago

https://github.com/paulirish/pwmetrics/pull/169