Rajat Gupta
Rajat Gupta

Reputation: 1

Can't generate JWT client with Google's node.js client library. Getting Typeerror while running the code

I'm trying to generate an access token via a JWT client using Google's node.js client library as mentioned here.

Here's the code snippet I'm following:

const { google } = require('googleapis')
const key = require('./auth.json')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new google.auth.JWT(key.client_email, null, key.private_key, scopes)
const view_id = 'Your View ID'

jwt.authorize((err, response) => {
  google.analytics('v3').data.ga.get(
    {
      auth: jwt,
      ids: 'ga:' + view_id,
      'start-date': '30daysAgo',
      'end-date': 'today',
      metrics: 'ga:pageviews'
    },
    (err, result) => {
      console.log(err, result)
    }
  )
})

But I keep getting this error message:

TypeError: Cannot read property 'auth' of undefined

Anyone know what's causing this?

Upvotes: 0

Views: 130

Answers (1)

Fan Yang
Fan Yang

Reputation: 11

You'll need google-auth-library: npm install google-auth-library

Upvotes: 1

Related Questions