如何在Facebook Messenger Bot中获取新的会话访问令牌

时间:2016-12-28 22:59:21

标签: node.js facebook-graph-api heroku facebook-messenger facebook-messenger-bot

我更改了我的Facebook帐户密码,我制作的Facebook Messenger Bot无法再回复我的邮件。

我的Heroku日志出现了这个错误:

2016-12-28T22:19:32.647313+00:00 heroku[router]: at=info method=POST path="/webhook" host=messengerrainbot.herokuapp.com request_id=0a347c11-4d67-4dcf-ae3b-2b8d4cc55b8d fwd="66.220.156.117" dyno=web.1 connect=0ms service=3ms status=200 bytes=196
2016-12-28T22:19:32.749540+00:00 app[web.1]: Error:  { message: 'Error validating access token: The session has been invalidated because the user has changed the password.',
2016-12-28T22:19:32.749560+00:00 app[web.1]:   type: 'OAuthException',
2016-12-28T22:19:32.749561+00:00 app[web.1]:   code: 190,
2016-12-28T22:19:32.749562+00:00 app[web.1]:   error_subcode: 460,
2016-12-28T22:19:32.749563+00:00 app[web.1]:   fbtrace_id: 'FlCFAaxy8j3' }

我找不到任何地方如何获取新的会话访问令牌。

我的index.js文件控制着一切:

var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.listen((process.env.PORT || 3000));

// Server frontpage
app.get('/', function (req, res) {
  res.send('This is TestBot Server');
});

// Facebook Webhook
app.get('/webhook', function (req, res) {
  if (req.query['hub.verify_token'] === 'EAAWWpxUBMGoBANlKHUR63Dz2WLZBMHACQRi65zwj46wFSb3aJCThUV4BLwevZCciUDeyVnYpGcfgrzu0reh3XqKdTkZBxVA1HhIfrAhbQjsy5ABYfUFTbfAdck4QmH0vJV6ZCAgAcGdrBl3pMaTQa2eCrPetlsZBHXAjZBIuISOgZDZD') {
    res.send(req.query['hub.challenge']);
  } else {
    res.send('Invalid verify token');
  }
});

// handler receiving messages
app.post('/webhook', function (req, res) {
  var events = req.body.entry[0].messaging;
  for (i = 0; i < events.length; i++) {
    var event = events[i];
    if (event.message && event.message.text) {
      sendMessage(event.sender.id, {text: "Hi. Send your location"}); // event.message.text
    } else if (event.message && event.message.attachments && event.message.attachments[0] && event.message.attachments[0].payload && event.message.attachments[0].payload.coordinates) {
      urlBase = "http://api.wunderground.com/api/57fd25cc02e9da86/conditions/forecast/alert/q/"
      lat = event.message.attachments[0].payload.coordinates.lat
      lon = event.message.attachments[0].payload.coordinates.long
      totUrl = urlBase + String(lat) + "," + String(lon) + ".json"

      request({
        url: totUrl,
        json: true
      }, function (error, response, body) {

        if (!error && response.statusCode === 200) {
          var rain = body.current_observation.precip_1hr_metric
          if (rain > 0) {
            sendMessage(event.sender.id, {text: "It's gonna rain. Grab an umbrella!"});
          } else {
            sendMessage(event.sender.id, {text: "No rain ahead!"});
          }
        }
      })
    } 
    events = []
  }
  req.body.entry[0].messaging = []
  res.sendStatus(200);
});

// generic function sending messages
function sendMessage(recipientId, message) {
  request({
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
    method: 'POST',
    json: {
      recipient: {id: recipientId},
      message: message,
    }
  }, function(error, response, body) {
    if (error) {
      console.log('Error sending message: ', error);
    } else if (response.body.error) {
      console.log('Error: ', response.body.error);
    }
  });
};

和链接 如果需要,my full Github repo

1 个答案:

答案 0 :(得分:0)

您可以在应用程序中生成新的访问令牌: