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

问题描述 投票:0回答:1

我更改了 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);
    }
  });
};

我完整的 Github 存储库

node.js facebook-graph-api heroku facebook-messenger facebook-messenger-bot
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.