ebay-oauth-nodejs-client
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -43,3 +43,4 @@ /* | ||
clientId: '---Client id ----', | ||
clientSecret: '-- client secret---' | ||
clientSecret: '-- client secret---', | ||
redirectUri: '-- redirect uri name --' | ||
}); | ||
@@ -58,2 +59,5 @@ | ||
// Using user consent url, you will be able to generate the code which you can use it for exchangeCodeForAccessToken. | ||
// Also accepts optional values: prompt, state | ||
ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scopes, { prompt: 'login', state: 'custom-state-value' }); | ||
// // Exchange Code for Authorization token | ||
@@ -60,0 +64,0 @@ ebayAuthToken.exchangeCodeForAccessToken('PRODUCTION', code).then((data) => { // eslint-disable-line no-undef |
{ | ||
"name": "ebay-oauth-nodejs-client", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Get oauth2 token for ebay developer application", | ||
@@ -41,2 +41,2 @@ "main": "src/index.js", | ||
} | ||
} | ||
} |
@@ -27,11 +27,12 @@ # Ebay Oauth Client | ||
OAuth 2.0 is the most widely used standard for authentication and authorization for API based access. The complete end to end documentation on how eBay OAuth functions may be used is available at developer.ebay.com. | ||
See e.g.,https://developer.ebay.com/api-docs/static/oauth-tokens.html | ||
See: https://developer.ebay.com/api-docs/static/oauth-tokens.html | ||
## Installation | ||
Using npm: | ||
```shell | ||
npm install ebay-oauth-nodejs-client | ||
``` | ||
or | ||
Using yarn: | ||
```shell | ||
@@ -43,10 +44,17 @@ yarn add ebay-oauth-nodejs-client | ||
##### EbayAuthToken(config) | ||
Create a new instance of `EbayAuthToken` with a relevant config. | ||
```js | ||
const EbayAuthToken = require('ebay-oauth-nodejs-client'); | ||
const ebayAuthToken = new EbayAuthToken({ | ||
clientId: '<your_client_id>', | ||
clientSecret: '<your_client_secret>'. | ||
clientSecret: '<your_client_secret>', | ||
redirectUri: '<redirect uri>' | ||
}); | ||
// generate client credential token | ||
``` | ||
##### ebayAuthToken.getApplicationToken(environment) | ||
Generate client credential token. | ||
```js | ||
(async () => { | ||
@@ -56,11 +64,29 @@ const token = await ebayAuthToken.getApplicationToken('PRODUCTION'); | ||
})(); | ||
``` | ||
// generate user consent authorization url. | ||
(async () => { | ||
const authUrl = await ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scopes); | ||
##### ebayAuthToken.generateUserAuthorizationUrl(environment, scopes[, options]) | ||
Generate user consent authorization url. | ||
```js | ||
(() => { | ||
const authUrl = ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scopes); | ||
console.log(authUrl); | ||
})(); | ||
``` | ||
// Getting a User access token. | ||
You can also provide optional values:\ | ||
**state:** An opaque value used by the client to maintain state between the request and callback.\ | ||
**prompt:** Force a user to log in when you redirect them to the Grant Application Access page, even if they already have an existing user session. | ||
The method call above could also be done as | ||
```js | ||
(() => { | ||
const options = { state: 'custom-state-value', prompt: 'login' }; | ||
const authUrl = ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scopes, options); | ||
console.log(authUrl); | ||
})(); | ||
``` | ||
##### ebayAuthToken.exchangeCodeForAccessToken(environment, code) | ||
Getting a User access token. | ||
```js | ||
(async () => { | ||
@@ -70,4 +96,7 @@ const accessToken = await ebayAuthToken.exchangeCodeForAccessToken('PRODUCTION', code); | ||
})(); | ||
``` | ||
// Using a refresh token to update a User access token (Updating the expired access token). | ||
##### ebayAuthToken.getAccessToken(environment, refreshToken, scopes) | ||
Using a refresh token to update a User access token (Updating the expired access token). | ||
```js | ||
(async () => { | ||
@@ -77,7 +106,7 @@ const accessToken = await ebayAuthToken.getAccessToken('PRODUCTION', refreshToken, scopes); | ||
})(); | ||
``` | ||
``` | ||
## Library Setup and getting started | ||
1. Invoke the outh ebay library as given below | ||
1. Invoke the oauth ebay library as given below | ||
```javascript | ||
@@ -93,3 +122,4 @@ const EbayAuthToken = require('ebay-oauth-nodejs-client'); | ||
clientId: '<your_client_id>', | ||
clientSecret: '<your_client_secret>' | ||
clientSecret: '<your_client_secret>', | ||
redirectUri: '<redirect_uri_name>' | ||
}); | ||
@@ -170,13 +200,4 @@ ``` | ||
Getting Client Id and Client Secret | ||
```shell | ||
https://developer.ebay.com/api-docs/static/oauth-credentials.html | ||
``` | ||
Getting your redirect_uri value | ||
```shell | ||
https://developer.ebay.com/api-docs/static/oauth-redirect-uri.html | ||
``` | ||
Specifying right scopes | ||
```shell | ||
https://developer.ebay.com/api-docs/static/oauth-scopes.html | ||
``` | ||
* Getting Client Id and Client Secret: https://developer.ebay.com/api-docs/static/oauth-credentials.html | ||
* Getting your redirect_uri value: https://developer.ebay.com/api-docs/static/oauth-redirect-uri.html | ||
* Specifying right scopes: https://developer.ebay.com/api-docs/static/oauth-scopes.html |
@@ -69,8 +69,10 @@ /* | ||
* 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. | ||
* @param {string} environment Environment (production/sandbox). | ||
* @param {string[]} scopes array list of scopes for which you need to generate the access token. | ||
* @param {Object} options optional values | ||
* @param {string} options.state custom state value | ||
* @param {string} options.prompt enforce to log in | ||
* @return userConsentUrl | ||
*/ | ||
generateUserAuthorizationUrl(environment, scopes, state) { | ||
generateUserAuthorizationUrl(environment, scopes, options) { | ||
validateParams(environment, scopes, this.credentials); | ||
@@ -80,3 +82,5 @@ const credentials = this.credentials[environment]; | ||
if (!credentials.redirectUri) throw new Error('redirect_uri is required for redirection after sign in \n kindly check here https://developer.ebay.com/api-docs/static/oauth-redirect-uri.html'); | ||
if (options && typeof(options) !== 'object') throw new Error('Improper way to provide optional values'); | ||
this.scope = Array.isArray(scopes) ? scopes.join('%20') : scopes; | ||
const { prompt, state } = options || {}; | ||
let queryParam = `client_id=${credentials.clientId}`; | ||
@@ -86,3 +90,3 @@ queryParam = `${queryParam}&redirect_uri=${credentials.redirectUri}`; | ||
queryParam = `${queryParam}&scope=${this.scope}`; | ||
queryParam = `${queryParam}&prompt=${this.prompt}`; | ||
queryParam = prompt ? `${queryParam}&prompt=${prompt}` : queryParam; | ||
queryParam = state ? `${queryParam}&state=${state}` : queryParam; | ||
@@ -89,0 +93,0 @@ const baseUrl = environment === 'PRODUCTION' ? consts.OAUTHENVIRONMENT_WEBENDPOINT_PRODUCTION |
@@ -66,3 +66,3 @@ 'use strict'; | ||
it('test generateUserAuthorizationUrl without prompt', () => { | ||
it('test generateUserAuthorizationUrl without options', () => { | ||
const ebayAuthToken = new EbayAuthToken({ | ||
@@ -72,6 +72,6 @@ filePath: 'test/test.json' | ||
const scope = 'https://api.ebay.com/oauth/api_scope'; | ||
expect(ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scope)).to.equal('https://auth.ebay.com/oauth2/authorize?client_id=PROD1234ABCD&redirect_uri=PRODREDIRECT&response_type=code&scope=https://api.ebay.com/oauth/api_scope&prompt=undefined'); | ||
expect(ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scope)).to.equal('https://auth.ebay.com/oauth2/authorize?client_id=PROD1234ABCD&redirect_uri=PRODREDIRECT&response_type=code&scope=https://api.ebay.com/oauth/api_scope'); | ||
}); | ||
it('test generateUserAuthorizationUrl with state', () => { | ||
it('test generateUserAuthorizationUrl with incorrect options', () => { | ||
const ebayAuthToken = new EbayAuthToken({ | ||
@@ -81,5 +81,16 @@ filePath: 'test/test.json' | ||
const scope = 'https://api.ebay.com/oauth/api_scope'; | ||
expect(ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scope, 'state')).to.equal('https://auth.ebay.com/oauth2/authorize?client_id=PROD1234ABCD&redirect_uri=PRODREDIRECT&response_type=code&scope=https://api.ebay.com/oauth/api_scope&prompt=undefined&state=state'); | ||
expect(() => { | ||
ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scope, 'options'); | ||
}).to.throw(Error, 'Improper way to provide optional values'); | ||
}); | ||
it('test generateUserAuthorizationUrl with options', () => { | ||
const ebayAuthToken = new EbayAuthToken({ | ||
filePath: 'test/test.json' | ||
}); | ||
const scope = 'https://api.ebay.com/oauth/api_scope'; | ||
const options = { prompt: 'login', state: 'state' }; | ||
expect(ebayAuthToken.generateUserAuthorizationUrl('PRODUCTION', scope, options)).to.equal('https://auth.ebay.com/oauth2/authorize?client_id=PROD1234ABCD&redirect_uri=PRODREDIRECT&response_type=code&scope=https://api.ebay.com/oauth/api_scope&prompt=login&state=state'); | ||
}); | ||
it('test generateUserAuthorizationUrl with sandbox env', () => { | ||
@@ -90,3 +101,3 @@ const ebayAuthToken = new EbayAuthToken({ | ||
const scope = 'https://api.ebay.com/oauth/api_scope'; | ||
expect(ebayAuthToken.generateUserAuthorizationUrl('SANDBOX', scope)).to.equal('https://auth.sandbox.ebay.com/oauth2/authorize?client_id=SAND1234ABCD&redirect_uri=SANDBOXREDIRECT&response_type=code&scope=https://api.ebay.com/oauth/api_scope&prompt=undefined'); | ||
expect(ebayAuthToken.generateUserAuthorizationUrl('SANDBOX', scope)).to.equal('https://auth.sandbox.ebay.com/oauth2/authorize?client_id=SAND1234ABCD&redirect_uri=SANDBOXREDIRECT&response_type=code&scope=https://api.ebay.com/oauth/api_scope'); | ||
}); | ||
@@ -93,0 +104,0 @@ |
40972
452
197