Skip to content

Commit

Permalink
Merge pull request #1 from ajay2507/access-token
Browse files Browse the repository at this point in the history
Access token
  • Loading branch information
pajaydev authored Mar 26, 2018
2 parents 844ffda + a22b401 commit e407689
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 11 deletions.
21 changes: 21 additions & 0 deletions demo/browseApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Ebay = require('../src/index');
let access_token = "";
let ebay = new Ebay({
clientID: "--Client ID ----",
clientSecret: '-- Client Secret----',
body: {
grant_type: "client_credentials",
scope: 'https://api.ebay.com/oauth/api_scope'

}
});
// Getting access token and calling getItem method.
ebay.getAccessToken()
.then((data) => {
ebay.getItem('v1|202117468662|0').then((data) => {
console.log(data);
// Data is in format of JSON
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
})
});

14 changes: 14 additions & 0 deletions demo/getAccessToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Ebay = require('../src/index');

let ebay = new Ebay({
clientID: "--Client Id----",
clientSecret: '-- Client Secret --',
body: {
grant_type: "client_credentials"
}
});
ebay.getAccessToken().then((data) => {
console.log(data);
}, (error) => {
console.log(error);
});
2 changes: 1 addition & 1 deletion demo/getAllCategories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let ebay = new Ebay({
});

ebay.getAllCategories().then((data) => {
console.log(data); //data.CategoryArray
console.log(data); //extract data.CategoryArray
}, (error) => {
console.log(error);
})
49 changes: 43 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
let makeRequest = require('./request');
let { getRequest, makeRequest, base64Encode } = require('./request');
let urlObject = require('./buildURL');

function Ebay(options) {
console.log(options);

if (!options) throw new Error("Options is missing, please provide the input");
if (!options.clientID) throw Error("Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/");
if (!(this instanceof Ebay)) return new Ebay(options);
this.options = options;
this.options.globalID = options.countryCode || "EBAY-US";
this.options.keyword = "iphone";
Expand All @@ -22,7 +23,7 @@ Ebay.prototype = {
this.options.param = "keywords";
let url = urlObject.buildSearchUrl(this.options);
console.log(url);
return makeRequest(url).then((data) => {
return getRequest(url).then((data) => {
let result = JSON.parse(data);
return result["findItemsByKeywordsResponse"];

Expand All @@ -39,7 +40,7 @@ Ebay.prototype = {
this.options.param = "categoryId";
let url = urlObject.buildSearchUrl(this.options);
console.log(url);
return makeRequest(url).then((data) => {
return getRequest(url).then((data) => {
let result = JSON.parse(data);
return result["findItemsByCategoryResponse"];

Expand All @@ -56,7 +57,7 @@ Ebay.prototype = {
this.options.param = "CategoryID";
let url = urlObject.buildShoppingUrl(this.options);
console.log(url);
return makeRequest(url).then((data) => {
return getRequest(url).then((data) => {
let result = JSON.parse(data);
return result;
}, (error) => {
Expand All @@ -67,7 +68,7 @@ Ebay.prototype = {
getVersion: function () {
this.options.operationName = "getVersion";
let url = urlObject.buildSearchUrl(this.options);
return makeRequest(url).then((data) => {
return getRequest(url).then((data) => {
let result = JSON.parse(data);
return result["getVersionResponse"][0];
}, (error) => {
Expand All @@ -83,13 +84,49 @@ Ebay.prototype = {
this.options.includeSelector = this.options.details ? "Details" : null;
let url = urlObject.buildShoppingUrl(this.options);
console.log(url);
return makeRequest(url).then((data) => {
return getRequest(url).then((data) => {
let result = JSON.parse(data);
console.log(result);
return result;
}, (error) => {
console.log(error);
})
},

getItem: function (itemId) {
console.log(this.options);
if (!itemId) throw new Error("Item Id is required");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
const id = encodeURIComponent(itemId);
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
console.log("Success");
let resultJSON = JSON.parse(result);
//this.setAccessToken(resultJSON);
return resultJSON;
});
},

setAccessToken: function (token) {
console.log("inside access tokeeeeee" + token);
this.options.access_token = token;
},

getAccessToken: function () {
if (!this.options.clientID) throw new Error("Missing Client ID");
if (!this.options.clientSecret) throw new Error("Missing Client Secret or Cert Id");
if (!this.options.body) throw new Error("Missing Body, required Grant type");
const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret);
let self = this;
console.log(this.options.body);
const auth = "Basic " + encodedStr;
return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
console.log("Successssssssss");
let resultJSON = JSON.parse(result);
// console.log(this);
self.setAccessToken(resultJSON.access_token);
return resultJSON;
});
}

};
Expand Down
51 changes: 47 additions & 4 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const https = require("http");
let httpRequest = require("https");
const qs = require("querystring");

let makeRequest = function makeRequest(url) {
let getRequest = function getRequest(url) {
if (url.includes("http://")) httpRequest = require("http");
return new Promise(function (resolve, reject) {
https.get(url, res => {
httpRequest.get(url, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
Expand All @@ -17,4 +19,45 @@ let makeRequest = function makeRequest(url) {

}

module.exports = makeRequest;
let makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
methodName == "POST" ? dataString = qs.stringify(data) : '';
// console.log(dataString);
const options = {
"hostname": hostName,
"path": endpoint,
"method": methodName || 'GET',
"headers": {
"content-type": methodName == "POST" ? "application/x-www-form-urlencoded" : "application/json",
"authorization": token,
"cache-control": "no-cache",
}
};
console.log("------------------------");
console.log(options);
return new Promise(function (resolve, reject) {
var req = httpRequest.request(options, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
//console.log(body);
});
res.on("end", () => {
resolve(body);

});
});
//console.log("request " + dataString);
if (methodName == "POST") req.write(dataString)
req.end();

})
}


let base64Encode = (encodeData) => {
let buff = new Buffer(encodeData);
return buff.toString('base64');
}

module.exports = { getRequest, makeRequest, base64Encode };

0 comments on commit e407689

Please sign in to comment.