[英]Shopify REST API Pagination using Node.js and Express
[英]Logging into Shopify using Node.js
我正在嘗試使用此處顯示的逆向工程說明將折扣代碼發布到用戶的商店, 網址為http://ma.rtin.so/reverse-engineering-shopify-private-apis (說明在PHP中)
第一步是正確登錄用戶帳戶,以便我可以從響應中獲取信息。 我是否正確執行此步驟? 我感覺好像缺少與令牌有關的內容,但是很難理解說明中給出的PHP代碼。
我從登錄功能接收到沒有錯誤狀態代碼的響應,但是我仍然不知道這是否表示我做得正確。 謝謝你的幫助。
use strict';
var request = require('request');
var cookie = require('cookie');
var USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17';
var login = function(req, res, cb) {
req.url = 'https://' + req.body.name + '.myshopify.com';
var post_data = {
'utf8': '✓',
'redirect': '',
'subdomain': req.body.name,
'login': req.body.email,
'password': req.body.pwd
}
var headers = {
'User-Agent': USER_AGENT,
'Content-Type': 'application/x-www-form-urlencoded'
};
var url = req.url + '/admin/auth/login';
request.post({ url: url, form: post_data, headers: headers }, function(err, response, body) {
if (err) 'ERROR LOGGING IN';
else {
if (response.statusCode !== 200) throw 'ERROR LOGGING IN';
console.log('Login response headers:', response.headers);
var shopCookies = response.headers['set-cookie'];
var j = request.jar();
for (var i = 0; i < shopCookies.length; i++) {
var cookie = request.cookie(shopCookies[i]);
j.setCookie(cookie, url);
}
req.cookie_string = j.getCookieString(url);
if (cb !== undefined) cb(req, res);
}
});
};
var setCoupons = function(req, res) {
var url = req.url + '/admin/discounts/new';
var headers = {
'User-Agent': USER_AGENT,
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': req.cookie_string
};
request.get({ url: url, headers: headers }, function(err, response, body) {
if (err) throw 'Problem setting coupons';
var value = response.body.match(/name="authenticity_token" value=".*"/i)[0];
var index = value.indexOf('value="');
value = (value.substring(index + 7, value.length - 1));
var count = parseInt(req.body.amount) + 1;
var checkCount = count;
var codes = [];
for (var i = 1; i < count; i++) {
var post_data = {
utf8: '✓',
authenticity_token: value,
discount: {
code: req.body.code + "_" + i,
discount_type: req.body.discount_type,
value: parseInt(req.body.value),
applies_to_resource: '',
starts_at: '2016-04-10'
},
'unlimited-uses': '',
discount_never_expires: ''
}
codes.push(post_data.discount);
var url = req.url + '/admin/discounts';
request.post({ url: url, form: post_data, headers: headers }, function(err, response, body) {
checkCount--;
if (checkCount < 2) {
res.send(codes);
}
});
}
});
};
export function create(req, res) {
login(req, res, setCoupons);
}
折扣代碼僅適用於Shopify Plus客戶。 但是該API現在可用: https : //docs.shopify.com/api/reference/discount
同樣,除了使用Shopify Plus之外,還可以將您自己的折扣代碼引擎創建為應用程序並將折扣應用於銷售訂單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.