应用程序拒绝连接(NodeJs 和 Express)

阿米尔·纳萨尔

我在合作伙伴面板中创建了一个应用程序,并且我遵循了这个文档(使用 Nodejs 和 Express)。

我可以毫无问题地获取产品对象的 JSON 格式。但是,当我添加到范围变量“read_price_rules”时,我收到此错误消息:“express-example-app 拒绝连接。”

这个问题是由应用的权限引起的吗?

我的应用程序可以: 阅读产品、变体和系列。

这是 index.js 文件:

const dotenv = require('dotenv').config();
const express = require('express');
const app = express();
const crypto = require('crypto');
const cookie = require('cookie');
const nonce = require('nonce')();
const querystring = require('querystring');
const request = require('request-promise');

const apiKey = process.env.SHOPIFY_API_KEY;
const apiSecret = process.env.SHOPIFY_API_SECRET;
const scopes = 'read_products,read_price_rules';
const forwardingAddress = "https://53b16008.ngrok.io";

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

app.get('/shopify', (req, res) => {
    const shop = req.query.shop;
    if (shop) {
      const state = nonce();
      const redirectUri = forwardingAddress + '/shopify/callback';
      const installUrl = 'https://' + shop + '/admin/oauth/authorize?client_id=' + apiKey + '&scope=' + scopes + '&state=' + state + '&redirect_uri=' + redirectUri;

      res.cookie('state', state);
      res.redirect(installUrl);
    } 
    else { return res.status(400).send('Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request'); }
});

app.get('/shopify/callback', (req, res) => {
  const { shop, hmac, code, state } = req.query;
  const stateCookie = cookie.parse(req.headers.cookie).state;

  if (state !== stateCookie) { return res.status(403).send('Request origin cannot be verified'); }

  if (shop && hmac && code) {
    // DONE: Validate request is from Shopify
    const map = Object.assign({}, req.query);
    delete map['signature'];
    delete map['hmac'];
    const message = querystring.stringify(map);
    const providedHmac = Buffer.from(hmac, 'utf-8');
    const generatedHash = Buffer.from(crypto.createHmac('sha256', apiSecret).update(message).digest('hex'), 'utf-8');
    let hashEquals = false;

    try { hashEquals = crypto.timingSafeEqual(generatedHash, providedHmac) }
    catch (e) { hashEquals = false; };

    if (!hashEquals) { return res.status(400).send('HMAC validation failed'); }

    // DONE: Exchange temporary code for a permanent access token
    const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
    const accessTokenPayload = {
      client_id: apiKey,
      client_secret: apiSecret,
      code,
    };

    request.post(accessTokenRequestUrl, { json: accessTokenPayload })
    .then((accessTokenResponse) => {
      const accessToken = accessTokenResponse.access_token;
      // DONE: Use access token to make API call to 'shop' endpoint
      const shopRequestUrl = 'https://' + shop + '/admin/api/2019-04/discount_codes/lookup.json?code=20OFF';
      const shopRequestHeaders = { 'X-Shopify-Access-Token': accessToken, };

      request.get(shopRequestUrl, { headers: shopRequestHeaders })
      .then((shopResponse) => {
        res.status(200).end(shopResponse);
      })
      .catch((error) => {
        res.status(error.statusCode).send(error.error.error_description);
      });
    })
    .catch((error) => {
      res.status(error.statusCode).send(error.error.error_description);
    });

  } else {
    res.status(400).send('Required parameters missing');
  }
});
阿米尔·纳萨尔

我只需要在 index.js 文件中添加额外的范围后重新安装应用程序。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NodeJS / Express应用程序的使用顺序和用法

使用CoffeeScript和HAML生成NodeJS Express应用程序

在NodeJS和Express应用程序中共享内存

CodePipeline + Beanstalk连接拒绝React + Express应用程序

如何使用nodeJS和express为OpenShift应用程序配置路由

如何将Typescript,NodeJS和Express应用程序部署到Heroku

需要 javascript 和 typescript 文件才能在 nodejs express 应用程序上工作

我应该使用哪个命令来最小化和优化nodejs express应用程序?

带有 nodejs /express 和 mongodb 的实时 CRUD 应用程序

如何使用 nodejs 和 express.js 创建一个 android 聊天应用程序?

通用输出目录,Web应用程序和IIS Express

使用Express和Ember启动Web应用程序

Mongodb 和 nodejs 与 express

如何使强大的不保存到 nodejs 和 express 应用程序上的 var/文件夹

构建一个聊天应用程序,NodeJS和Express-媒体流应该使用什么?

将Express与React应用程序连接

Docker 组合 Redis 和 Spring 启动应用程序:java.net.ConnectException:连接被拒绝(连接被拒绝)",

WebView应用程序和拒绝

为什么我的 React-Express 应用程序无法从 Express 后端获取和显示数据?

连接到 nodejs (express) 被拒绝

连接到通过 Nginx 运行 express 的 Nodejs 应用程序时出现 502 Bad Gateway

Express应用程序中未处理的拒绝

尝试与 Webpack 和 Express 同时开发服务器和客户端应用程序

Docker for NodeJS Express和Python

获取“ TypeError:必须提供秘密字符串”。在带有Passport.js和Express-Session作为中间件的NodeJS应用程序中

Monoid和应用程序如何连接?

ssh 和 nodejs 应用程序

使用express.js和node.js运行我的示例应用程序

Express会话-在React应用程序的req之外添加和访问自定义cookie字段