Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples in readme file and support travis ci #5

Merged
merged 7 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
os:
- linux
- osx
node_js:
- "node"
- 10
- 8
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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: '<your_client_id>',
clientSecret: '<your_client_secret>'.
redirectUri: '<redirect uri>'
});
// 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

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
29 changes: 28 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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');
Expand All @@ -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);
Expand Down