From 901f8a2769293332953c9073bee20f6e07323369 Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Mon, 16 Mar 2020 12:54:15 -0700 Subject: [PATCH] Added examples in readme file and support travis ci (#5) * pass credentials in constructor * update readme * added better documentation in read me * added travis support * update read me * update readme --- .travis.yml | 8 ++++++++ README.md | 38 ++++++++++++++++++++++++++++++++++++++ package.json | 4 ++-- src/constants.js | 2 +- src/index.js | 29 ++++++++++++++++++++++++++++- 5 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8f5e0d4 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: node_js +os: + - linux + - osx +node_js: + - "node" + - 10 + - 8 \ No newline at end of file diff --git a/README.md b/README.md index eb81ffd..145da34 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ This code allows developers to fetch an OAuth token that can be used to call the eBay Developer REST APIs. The code is intended for use with Node.js. [![npm version](https://badge.fury.io/js/ebay-oauth-nodejs-client.svg)](https://badge.fury.io/js/ebay-oauth-nodejs-client) +[![Build Status](https://travis-ci.com/eBay/ebay-oauth-nodejs-client.svg?branch=master)](https://travis-ci.com/github/eBay/ebay-oauth-nodejs-client) + ## Table of Contents @@ -33,6 +35,42 @@ or ```shell yarn add ebay-oauth-nodejs-client +``` + +## Usage + +```js +const EbayAuthToken = require('ebay-oauth-nodejs-client'); +const ebayAuthToken = new EbayAuthToken({ + clientId: '', + clientSecret: ''. + redirectUri: '' +}); +// generate client credential token +(async () => { + const token = await ebayAuthToken.getApplicationToken('PRODUCTION'); + console.log(token); +})(); + +// generate user consent authorization url. +(async () => { + const authUrl = await ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scopes); + console.log(authUrl); +})(); + +// Getting a User access token. + +(async () => { + const accessToken = await ebayAuthToken.exchangeCodeForAccessToken('PRODUCTION', code); + console.log(accessToken); +})(); + +// Using a refresh token to update a User access token (Updating the expired access token). +(async () => { + const accessToken = await ebayAuthToken.getAccessToken('PRODUCTION', refreshToken, scopes); + console.log(accessToken); +})(); + ``` ## Library Setup and getting started diff --git a/package.json b/package.json index 729f798..f9d7d4c 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "ebay-oauth-nodejs-client", - "version": "1.1.0", + "version": "1.2.0", "description": "Get oauth2 token for ebay developer application", "main": "src/index.js", "scripts": { "lint": "eslint .", - "test": "mocha" + "test": "eslint . && mocha" }, "author": "Ajaykumar Prathap", "license": "ISC", diff --git a/src/constants.js b/src/constants.js index 78712ad..f00c96c 100644 --- a/src/constants.js +++ b/src/constants.js @@ -22,7 +22,7 @@ module.exports.OAUTHENVIRONMENT_APIENDPOINT_SANDBOX = 'https://api.sandbox.ebay. module.exports.OAUTHENVIRONMENT_APIENDPOINT_PRODUCTION = 'https://api.ebay.com/identity/v1/oauth2/token'; // Scopes -module.exports.API_SCOPE_SANDBOX = 'https://api.ebay.com/oauth/api_scope'; +module.exports.CLIENT_CRED_SCOPE = 'https://api.ebay.com/oauth/api_scope'; // Environments module.exports.PROD_ENV = 'PRODUCTION'; diff --git a/src/index.js b/src/index.js index 36bd464..3911d43 100644 --- a/src/index.js +++ b/src/index.js @@ -41,12 +41,19 @@ class EbayOauthToken { if (!options) { throw new Error('This method accepts an object with filepath or with client id and client secret'); } + // get user credentials. this.credentials = options.filePath ? readJSONFile(options.filePath) : readOptions(options); this.grantType = ''; return this; } - getApplicationToken(environment, scopes) { + /** + * generate application access toke for client credentials grant flow. + * @param environment Environment (production/sandbox). + * @param scopes array list of scopes for which you need to generate the access token. + * @return accessToken object. + */ + getApplicationToken(environment, scopes = consts.CLIENT_CRED_SCOPE) { validateParams(environment, scopes, this.credentials); this.grantType = consts.PAYLOAD_VALUE_CLIENT_CREDENTIALS; this.scope = Array.isArray(scopes) ? scopes.join('%20') : scopes; @@ -57,6 +64,13 @@ class EbayOauthToken { return postRequest(data, this.credentials[environment]); } + /** + * generate user consent authorization url. + * @param environment Environment (production/sandbox). + * @param scopes array list of scopes for which you need to generate the access token. + * @param state custom state value. + * @return userConsentUrl + */ generateUserAuthorizationUrl(environment, scopes, state) { validateParams(environment, scopes, this.credentials); const credentials = this.credentials[environment]; @@ -74,6 +88,12 @@ class EbayOauthToken { return `${baseUrl}?${queryParam}`; } + /** + * Getting a User access token. + * @param environment Environment (production/sandbox). + * @param code code generated from browser using the method generateUserAuthorizationUrl. + * @return accessToken object. + */ exchangeCodeForAccessToken(environment, code) { if (!code) { throw new Error('Authorization code is required'); @@ -84,6 +104,13 @@ class EbayOauthToken { return postRequest(data, credentials); } + /** + * Using a refresh token to update a User access token (Updating the expired access token). + * @param environment Environment (production/sandbox). + * @param refreshToken refresh token. + * @param scopes array list of scopes for which you need to generate the access token. + * @return accessToken object. + */ getAccessToken(environment, refreshToken, scopes) { const token = refreshToken || this.getRefreshToken(); validateParams(environment, scopes, this.credentials);