Access token undefined

2019-08-27 01:22发布

问题:

i am new to google actions and dialogflow. Here i want to send notification to google assistant. But i am getting

TypeError: Cannot read property 'access_token' of undefined

 const { google } = require('googleapis');
 const key = require('./myapp.json');

let jwtClient = new google.auth.JWT(
 key.client_email, null, key.private_key,
 ['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
 null
 );

jwtClient.authorize((err, tokens) => {
let notif = {
    userNotification: {
        title: "message",
    },
    target: {
        userId: 'xxxyyyzzzz',
        intent: 'intent_name',
        locale: 'en-US'
    },
};

request.post('https://actions.googleapis.com/v2/conversations:send', {
    'auth': {
        'bearer': tokens.access_token,
    },
    'json': true,
    'body': { 'customPushMessage': notif },
}, (err, httpResponse, body) => {
    console.log(body);
    console.log(httpResponse.statusCode + ': ' + httpResponse.statusMessage);
  });
});