shopify-node
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -12,3 +12,4 @@ function ShopifyConnect(params) { | ||
secret: '', | ||
redirect: '' | ||
redirect: '', | ||
access_token: null | ||
}; | ||
@@ -18,2 +19,7 @@ | ||
$scope.setAccessToken = function(access_token) { | ||
$scope.config.access_token = access_token; | ||
return $scope; | ||
}; | ||
$scope.createURL = function() { | ||
@@ -54,2 +60,7 @@ return 'https://' + $scope.config.shop_name + '.myshopify.com/admin/oauth/authorize?' | ||
data += chunk; | ||
var j = JSON.parse(data); | ||
if(j.error) { | ||
return callback(j, null); | ||
} | ||
$scope.config.access_token = j.access_token; | ||
callback(null, data); | ||
@@ -60,7 +71,3 @@ }); | ||
shReq.on('error', function(e) { | ||
console.log(e); | ||
callback({ | ||
error: true, | ||
message: 'There was an error connecting to the authentication server' | ||
}, null); | ||
callback(e, null); | ||
}); | ||
@@ -72,2 +79,71 @@ | ||
$scope.request = function(access_token, path, method, data, callback) { | ||
if(access_token === null) { | ||
return callback({ | ||
error: true, | ||
message: 'No access token set.' | ||
}, null); | ||
} | ||
var q = JSON.stringify(data); | ||
method = method.toLowerCase(); | ||
var opts = { | ||
host: $scope.config.shop_name + '.myshopify.com', | ||
path: path, | ||
method: method, | ||
port: 443, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'Vern.io (http://www.typefoo.com)', | ||
'X-Shopify-Access-Token': access_token | ||
} | ||
}; | ||
if(method === 'post' || method === 'put') { | ||
opts.headers['Content-Length'] = q.length; | ||
} | ||
var req = https.request(opts, function(sock) { | ||
var fullData = ''; | ||
sock.setEncoding('utf-8'); | ||
sock.on('data', function(chunk) { | ||
fullData += chunk; | ||
}); | ||
sock.on('end', function() { | ||
var j = JSON.parse(fullData); | ||
callback(null, j); | ||
}); | ||
}); | ||
req.on('error', function(e) { | ||
callback(e, null); | ||
}); | ||
if(method === 'post' || method === 'put') { | ||
req.write(q); | ||
} | ||
req.end(); | ||
}; | ||
$scope.get = function(path, callback) { | ||
$scope.request($scope.config.access_token, path, 'get', null, callback); | ||
}; | ||
$scope.post = function(path, data, callback) { | ||
$scope.request($scope.config.access_token, path, 'post', data, callback); | ||
}; | ||
$scope.put = function(path, data, callback) { | ||
$scope.request($scope.config.access_token, path, 'put', data, callback); | ||
}; | ||
$scope.delete = function(path, callback) { | ||
$scope.request($scope.config.access_token, path, 'delete', null, callback); | ||
}; | ||
return $scope; | ||
@@ -74,0 +150,0 @@ } |
{ | ||
"name": "shopify-node", | ||
"description": "A NodeJS connector for Shopify OAuth2", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"author": { | ||
@@ -30,4 +30,4 @@ "name": "uh-sem-blee, Co. | typefoo", | ||
}, | ||
"_id": "shopify-node@0.1.3", | ||
"_id": "shopify-node@0.1.4", | ||
"_from": "shopify-node@" | ||
} |
@@ -25,3 +25,3 @@ # Shopify Node | ||
After you have obtained the "code" (either via your redirect or elsewhere): | ||
After you have obtained the 'code' (either via your redirect or elsewhere): | ||
@@ -34,3 +34,43 @@ var code = ''; // put the short-time auth code in here. | ||
}); | ||
If you have saved your access token in some sort of session or cookie data, you can skip the authorization request: | ||
var shopify = new shopifyObj({ | ||
shop_name: 'typefoo', | ||
id: '639e5b59d03a4135d4f4cd176d8b0d0c', | ||
secret: '07e3e4d5711054ead625ac7356552660', | ||
redirect: 'http://localhost:9000/#/oauth' | ||
access_token: '' // your access token to be used | ||
}); | ||
Once authorized, you can perform typical REST services (http://docs.shopify.com/api/ for reference): | ||
shopify.get('/admin/orders.json', function(err, resp) { | ||
console.log(resp); | ||
}); | ||
var postData = { | ||
product: { | ||
title: 'Burton Custom Freestlye 151', | ||
body_html: '<strong>Good snowboard!</strong>', | ||
vendor: 'Burton', | ||
product_type: 'Snowboard', | ||
variants: [ | ||
{ | ||
option1: 'First', | ||
price: '10.00', | ||
sku: 123 | ||
}, | ||
{ | ||
option1: 'Second', | ||
price: '20.00', | ||
sku: '123' | ||
} | ||
] | ||
} | ||
}; | ||
shopify.post('/admin/products.json', postData, function(err, resp) { | ||
console.log(resp); | ||
}); | ||
Built in Carolina & Ohio. www.typefoo.com |
var prompt = require('prompt'); | ||
var https = require('https'); | ||
var shopifyObj = require('../lib/shopify'); | ||
@@ -18,12 +19,65 @@ | ||
{ | ||
name: 'code', | ||
message: 'Enter the code parameter from the returned URL', | ||
required: true | ||
name: 'access_token', | ||
message: 'Enter access token (optional)' | ||
} | ||
], function(err, result) { | ||
console.log('\n\nConnecting to Shopify, retrieving Access Token...\n\n'); | ||
shopify.getAccessToken(result.code, function(err, access_token) { | ||
console.log(err); | ||
console.log(JSON.parse(access_token)); | ||
if(result.access_token) { | ||
shopify.setAccessToken(result.access_token); | ||
return sampleCalls(); | ||
} | ||
doAuthorization(); | ||
}); | ||
function doAuthorization() { | ||
prompt.get([ | ||
{ | ||
name: 'code', | ||
message: 'Enter the code parameter from the returned URL', | ||
required: true | ||
} | ||
], function(err, result) { | ||
console.log('\n\nConnecting to Shopify, retrieving Access Token...\n\n'); | ||
shopify.getAccessToken(result.code, function(err, access_token) { | ||
if(err) { | ||
console.log(err); | ||
} | ||
console.log(access_token); | ||
}); | ||
}); | ||
}); | ||
} | ||
function sampleCalls() { | ||
// GET | ||
shopify.get('/admin/orders.json', function(err, resp) { | ||
console.log(resp); | ||
}); | ||
// POST | ||
var postData = { | ||
product: { | ||
title: 'Burton Custom Freestlye 151', | ||
body_html: '<strong>Good snowboard!</strong>', | ||
vendor: 'Burton', | ||
product_type: 'Snowboard', | ||
variants: [ | ||
{ | ||
option1: 'First', | ||
price: '10.00', | ||
sku: 123 | ||
}, | ||
{ | ||
option1: 'Second', | ||
price: '20.00', | ||
sku: '123' | ||
} | ||
] | ||
} | ||
}; | ||
shopify.post('/admin/products.json', postData, function(err, resp) { | ||
console.log(resp); | ||
}); | ||
} |
8797
205
74
2