I am working on authorizing a JWT token using the googleapis node package. I am following the example from this link: Here If I change the variable name of the imported package it will yield an error.
Why does example 1 work but example 2 yields below error:
const jwt = new googleapi.auth.JWT(
^
TypeError: Cannot read property 'auth' of undefined
Example 1
'use strict'
const { google } = require('googleapis')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new google.auth.JWT(
process.env.CLIENT_EMAIL,
null,
process.env.PRIVATE_KEY,
scopes
)
const view_id = 'XXXXXXX'
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)
}
)
})
Example 2
'use strict'
const { googleapi } = require('googleapis')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new googleapi.auth.JWT(
process.env.CLIENT_EMAIL,
null,
process.env.PRIVATE_KEY,
scopes
)
const view_id = 'XXXXXXX'
jwt.authorize((err, response) => {
googleapi.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)
}
)
})