Facebook Graph API-Instagram-页面公共内容访问

时间:2018-09-12 08:11:27

标签: javascript node.js express facebook-graph-api instagram-graph-api

我正在NodeJS中开发一个API,以在自己的页面中显示我的社交网络供稿。 我可以显示Facebook,Twitter和Instagram(通过Facebook Graph API),但最后一个不显示并返回错误:

  

sdk.js:122

     

未捕获的TypeError:无法读取未定义的属性'id'

     

at:19:55

     

在a .__ wrapper.a .__ wrapper(sdk.js:122)

     

在F(sdk.js:139)

     

在Object.b.onload(sdk.js:134)

     

在sdk.js:134

     sdk.js中的

:50       在XMLHttpRequest.c.onreadystatechange(sdk.js:134)

仅当我将图像添加到页面时才会发生。由于图像处理有些特殊,因此我认为Facebook SDK和我的get paths节点之间存在URL冲突。

我的代码:

const express = require('express');
const https = require('https');
const fs = require('fs');
const app = express();

const config = require('./config');

var webId;

var options = {
  key: fs.readFileSync(config.paths.certificate.key),
  cert: fs.readFileSync(config.paths.certificate.crt),
  requestCert: false,
  rejectUnauthorized: false
};

app.use(function (req, res, next) {
  console.log(req.url);

  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Credentials", "true");
  res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT");
  res.setHeader("Access-Control-Allow-Headers", "Accept, Access-Control-Allow-Headers, Access-Control-Request-Headers, Access-Control-Request-Method, Authorization, Content-Type, Origin, X-Requested-With");
  next();
});

/**
 * HTTP GET for skip favicon request
 */
app.get('/favicon.ico', function(req, res) {
  res.status(404).send('No favicon found');
});

/**
 * HTTP GET for views from base path
 */
app.get(/^(\/\w+.\b(html|htm|xhtml)\b)/g, function(req, res) {
  let lastBar = req.url.lastIndexOf('/');
  let page = req.url.substr(lastBar);

  res.sendFile(page, {root: config.paths.webs + webId});
});

/**
 * HTTP GET for resources (images)
 */
app.get(/^(\/\b(web_resources)\b)/g, function(req, res) {
  if (typeof(webId) === undefined) {
    webId = req.params.id; 
  }
  res.sendFile(req.url, {root: config.paths.resources});
});

/**
 * HTTP GET for files (.js, .css, etc.) from another folder level
 */
app.get('/:folder/:file', function(req, res) {
  if (typeof(webId) === undefined) {
    webId = req.params.id; 
  }
  let folder = req.params.folder;
  let file = req.params.file;

  res.sendFile(file, {root: config.paths.webs + webId + '/' + folder});
});

/**
 * HTTP GET for .js files from the base path
 */
app.get(/\/\w+.\b(js)/, function(req, res) {
  let lastBar = req.url.lastIndexOf('/');
  let script = req.url.substr(lastBar);

  res.sendFile(script, {root: config.paths.webs});
});

/**
 * HTTP GET for index page
 */
app.get('/:id', function(req, res) {
  webId = req.params.id;
  res.sendFile('index.html', {root: config.paths.webs + webId});
});

/**
 * Server creation listening at secure port 443
 */
https.createServer(options, app).listen(443, function() {
  console.clear();
  console.log("NodeJS secure server started at port 443");
});

更新

我可以将问题简化为一段精确的代码:

FB.getLoginStatus(function (response) {
  if (response.status === "connected") {
    FB.api(
      "/" + id + "/",
      "GET", { "fields": "instagram_business_account" },
      // ***** THE PROBLEM IS HERE ***** //
      function (response) {
        if (response.error && response.error !== '') {
          console.log("Error recovering 'instagram_business_account':");
          console.log(response.error);
        } else {
          FB.api(
            "/" + response.instagram_business_account.id + "/media",
            "GET", { "fields": "shortcode" },
            function (response) {
              for (var i = 0; i < response.data.length; i++) {
                var xhttp = new XMLHttpRequest();

                xhttp.onreadystatechange = function() {
                  if (this.readyState == 4 && this.status == 200) {
                    html = JSON.parse(this.response).html.replace('<script async defer src="//www.instagram.com/embed.js"></script>', '');
                    document.getElementById("ig-posts").innerHTML += html;
                  }
                };

                xhttp.open("GET", "https://api.instagram.com/oembed/?url=http://instagr.am/p/" + response.data[i].shortcode + "/", true);
                xhttp.send();
              }
            }
          );
        }

      }
    );
  } else {
    console.log("Error recovering access token: Not connected.")
    console.log(response)
  }
});

正如我在代码中指出的那样,问题在于我无法获得“ instagram_business_account”,原因是:

  

(#10)要使用“页面公共内容访问”,请使用此端点   必须由Facebook审核和批准。提交此“公开页面”   内容访问”功能以供审核,请阅读以下内容的文档   可审查的功能:https://developers.facebook.com/docs/apps/review

这可能有道理,但是只有当我在要显示我的Instagram提要的页面上添加图像时,这种情况才会发生。否则,它会完美运行。

0 个答案:

没有答案