Angular4&Node.js-获取Instragram的API重定向结果

时间:2018-08-03 08:12:36

标签: json node.js angular instagram instagram-api

我正在开发Angular APP和NodeJS API,以显示按需提供社交网络,并且当Angular APP拨打电话时,Instagram的API(instagram-node)面临两步重定向问题。< / p>

在NodeJS部分,一切似乎都正常运行

端点

// For the Angular APP to call
app.get('/instagram/:clientId', (request, response) => {
  dataAccess.getInstagramTimeline(request, response, request.params.clientId);
});

// For Instagram`s auth redirection
app.get('/handleAuth', function(req, res) {
  dataAccess.instagramHandleAuth(req, res);
});

中产阶级

async function getInstagramTimeline(req, res, clientId) {
  await instagram.getTimeline(req, res, clientId);
}

async function instagramHandleAuth(req, res) {
  await instagram.handleAuth(req, res);
}

Instagram.js

async function getTimeline(req, res, clientId) {
  return await new Promise(async (resolve, reject) => {
    try {
      config = await databaseUtils.getConfig(clientId, 'instagram');

      if(typeof(config) !== 'undefined') {
        ig.use({
          client_id: config.clientID,
          client_secret: config.clientSecret
        });

        resolve(await res.redirect(ig.get_authorization_url(config.redirectUri, { scope: ['public_content', 'likes'] })));
      } else {
        reject('Instagram is not configured');
      }
    } catch (e) {
      reject(e.message);
    }
  });
}

async function handleAuth(req, res) {
  return await new Promise((resolve, reject) => {
    if(typeof(config) !== 'undefined') {
      ig.authorize_user(req.query.code, config.redirectUri, function(err, result) {
        if(err) {
          reject(err);
        } else {
          request('https://api.instagram.com/v1/users/self/media/recent?access_token=' + result.access_token, function(error, response, body) {
            if (error) {
              res.status(500).send(error);
            } else {
              res.setHeader('Content-Type', 'application/json');
              res.write(body);
              res.end();
              resolve();
            }
          });
        }
      });
    } else {
      reject('Instagram is not configured');
    }
  });
}

致电http://localhost:3001/instagram/1时的结果:

enter image description here

但是,当从Angular APP中访问时:

致电

async getInstagramFeed(clientId) {
    var url = this.apiUrl + 'instagram/' +  + clientId;

    await this.http.get(url)
    // .pipe(
    //   map((res: Response) => res.json())
    // )
    .subscribe(data => {
      console.log(data);
      return data;
    });
  }

结果

enter image description here

我猜测问题出在Instagram的API重定向上,以获取Angular APP无法处理的access_token。由于它调用http://localhost:3001/instagram/1,但最终的URI是http://localhost:3001/handleAuth

您知道如何像从API中一样从应用程序中获取最终的JSON吗?

0 个答案:

没有答案