简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM