Skip to content

Commit e407689

Browse files
authored
Merge pull request #1 from ajay2507/access-token
Access token
2 parents 844ffda + a22b401 commit e407689

File tree

5 files changed

+126
-11
lines changed

5 files changed

+126
-11
lines changed

demo/browseApi.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Ebay = require('../src/index');
2+
let access_token = "";
3+
let ebay = new Ebay({
4+
clientID: "--Client ID ----",
5+
clientSecret: '-- Client Secret----',
6+
body: {
7+
grant_type: "client_credentials",
8+
scope: 'https://api.ebay.com/oauth/api_scope'
9+
10+
}
11+
});
12+
// Getting access token and calling getItem method.
13+
ebay.getAccessToken()
14+
.then((data) => {
15+
ebay.getItem('v1|202117468662|0').then((data) => {
16+
console.log(data);
17+
// Data is in format of JSON
18+
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
19+
})
20+
});
21+

demo/getAccessToken.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const Ebay = require('../src/index');
2+
3+
let ebay = new Ebay({
4+
clientID: "--Client Id----",
5+
clientSecret: '-- Client Secret --',
6+
body: {
7+
grant_type: "client_credentials"
8+
}
9+
});
10+
ebay.getAccessToken().then((data) => {
11+
console.log(data);
12+
}, (error) => {
13+
console.log(error);
14+
});

demo/getAllCategories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let ebay = new Ebay({
66
});
77

88
ebay.getAllCategories().then((data) => {
9-
console.log(data); //data.CategoryArray
9+
console.log(data); //extract data.CategoryArray
1010
}, (error) => {
1111
console.log(error);
1212
})

src/index.js

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
2-
let makeRequest = require('./request');
2+
let { getRequest, makeRequest, base64Encode } = require('./request');
33
let urlObject = require('./buildURL');
44

55
function Ebay(options) {
66
console.log(options);
77

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

@@ -39,7 +40,7 @@ Ebay.prototype = {
3940
this.options.param = "categoryId";
4041
let url = urlObject.buildSearchUrl(this.options);
4142
console.log(url);
42-
return makeRequest(url).then((data) => {
43+
return getRequest(url).then((data) => {
4344
let result = JSON.parse(data);
4445
return result["findItemsByCategoryResponse"];
4546

@@ -56,7 +57,7 @@ Ebay.prototype = {
5657
this.options.param = "CategoryID";
5758
let url = urlObject.buildShoppingUrl(this.options);
5859
console.log(url);
59-
return makeRequest(url).then((data) => {
60+
return getRequest(url).then((data) => {
6061
let result = JSON.parse(data);
6162
return result;
6263
}, (error) => {
@@ -67,7 +68,7 @@ Ebay.prototype = {
6768
getVersion: function () {
6869
this.options.operationName = "getVersion";
6970
let url = urlObject.buildSearchUrl(this.options);
70-
return makeRequest(url).then((data) => {
71+
return getRequest(url).then((data) => {
7172
let result = JSON.parse(data);
7273
return result["getVersionResponse"][0];
7374
}, (error) => {
@@ -83,13 +84,49 @@ Ebay.prototype = {
8384
this.options.includeSelector = this.options.details ? "Details" : null;
8485
let url = urlObject.buildShoppingUrl(this.options);
8586
console.log(url);
86-
return makeRequest(url).then((data) => {
87+
return getRequest(url).then((data) => {
8788
let result = JSON.parse(data);
8889
console.log(result);
8990
return result;
9091
}, (error) => {
9192
console.log(error);
9293
})
94+
},
95+
96+
getItem: function (itemId) {
97+
console.log(this.options);
98+
if (!itemId) throw new Error("Item Id is required");
99+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
100+
const auth = "Bearer " + this.options.access_token;
101+
const id = encodeURIComponent(itemId);
102+
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
103+
console.log("Success");
104+
let resultJSON = JSON.parse(result);
105+
//this.setAccessToken(resultJSON);
106+
return resultJSON;
107+
});
108+
},
109+
110+
setAccessToken: function (token) {
111+
console.log("inside access tokeeeeee" + token);
112+
this.options.access_token = token;
113+
},
114+
115+
getAccessToken: function () {
116+
if (!this.options.clientID) throw new Error("Missing Client ID");
117+
if (!this.options.clientSecret) throw new Error("Missing Client Secret or Cert Id");
118+
if (!this.options.body) throw new Error("Missing Body, required Grant type");
119+
const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret);
120+
let self = this;
121+
console.log(this.options.body);
122+
const auth = "Basic " + encodedStr;
123+
return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
124+
console.log("Successssssssss");
125+
let resultJSON = JSON.parse(result);
126+
// console.log(this);
127+
self.setAccessToken(resultJSON.access_token);
128+
return resultJSON;
129+
});
93130
}
94131

95132
};

src/request.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const https = require("http");
1+
let httpRequest = require("https");
2+
const qs = require("querystring");
23

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

1820
}
1921

20-
module.exports = makeRequest;
22+
let makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
23+
methodName == "POST" ? dataString = qs.stringify(data) : '';
24+
// console.log(dataString);
25+
const options = {
26+
"hostname": hostName,
27+
"path": endpoint,
28+
"method": methodName || 'GET',
29+
"headers": {
30+
"content-type": methodName == "POST" ? "application/x-www-form-urlencoded" : "application/json",
31+
"authorization": token,
32+
"cache-control": "no-cache",
33+
}
34+
};
35+
console.log("------------------------");
36+
console.log(options);
37+
return new Promise(function (resolve, reject) {
38+
var req = httpRequest.request(options, res => {
39+
res.setEncoding("utf8");
40+
let body = "";
41+
res.on("data", data => {
42+
body += data;
43+
//console.log(body);
44+
});
45+
res.on("end", () => {
46+
resolve(body);
47+
48+
});
49+
});
50+
//console.log("request " + dataString);
51+
if (methodName == "POST") req.write(dataString)
52+
req.end();
53+
54+
})
55+
}
56+
57+
58+
let base64Encode = (encodeData) => {
59+
let buff = new Buffer(encodeData);
60+
return buff.toString('base64');
61+
}
62+
63+
module.exports = { getRequest, makeRequest, base64Encode };

0 commit comments

Comments
 (0)