Skip to content

Commit 06c4723

Browse files
authored
API-2196 support oauth client credentials js sdk (#132)
1 parent 6888169 commit 06c4723

6 files changed

+134
-0
lines changed

Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:11
2+
3+
RUN mkdir /app
4+
WORKDIR /app
5+
6+
COPY . /app/
7+
8+
RUN npm install

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.PHONY: run-docker
2+
run-docker:
3+
docker-compose up -d
4+
5+
.PHONY: stop-docker
6+
stop-docker:
7+
docker-compose down
8+
9+
# make executeSdkSample sample-file-name=oauth_client_credentials.js
10+
.PHONY: executeSdkSample
11+
executeSdkSample:
12+
docker-compose exec bynder-js-sdk node /app/samples/$(sample-file-name)

README.md

100644100755
+49
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ const bynder = new Bynder({
8686
});
8787
```
8888

89+
#### OAuth2 Client Credentials
90+
91+
OAuth application within Bynder needs client credentials grant type. Bynder object can be instantiated without redirectUri provided:
92+
93+
```js
94+
const bynder = new Bynder({
95+
baseURL: "http://api-url.bynder.io/api/",
96+
clientId: "<your OAuth2 client id>",
97+
clientSecret: "<your OAuth2 client secret>"
98+
});
99+
```
100+
101+
To get a token via client credentials, make a call to `getTokenClientCredentials()`:
102+
103+
```js
104+
const bynder = new Bynder({
105+
baseURL: "http://api-url.bynder.io/api/",
106+
clientId: "<your OAuth2 client id>",
107+
clientSecret: "<your OAuth2 client secret>"
108+
});
109+
const token = await bynder.getTokenClientCredentials();
110+
```
111+
112+
Sample call can be found within `samples/oauth_client_credentials.js`.
113+
89114
#### Making requests
90115

91116
You can now use the various methods from the SDK to fetch media, metaproperties
@@ -117,6 +142,7 @@ bynder
117142

118143
- `makeAuthorizationURL()`
119144
- `getToken()`
145+
- `getTokenClientCredentials()`
120146

121147
### Media
122148

@@ -195,3 +221,26 @@ are the steps necessary to prepare your environment:
195221
- `gulp doc` - Run JSDoc to create a 'doc' folder with automatically generated documentation for the source code.
196222
- `gulp webserver` - Deploy a web server from the root folder at
197223
`localhost:8080` to run the html samples (in order to avoid CORS problems).
224+
225+
## Docker Setup
226+
227+
228+
JavaScript files can be executed within a Docker container with corresponding dependencies installed.
229+
230+
### Initial Setup
231+
232+
From the root directory create a `secret.json` file.
233+
234+
```json
235+
{
236+
"baseURL": "http://api-url.bynder.io/api/",
237+
"clientId": "<your OAuth2 client id>",
238+
"clientSecret": "<your OAuth2 client secret>"
239+
}
240+
```
241+
242+
Run the command `make run-docker` to start up Docker container.
243+
244+
With the container running, execute the command:
245+
246+
`make executeSdkSample sample-file-name=oauth_client_credentials.js` where sample-file-name is the name of the file within `samples/`. This command will execute the JavaScript file within the container.

docker-compose.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
bynder-js-sdk:
3+
container_name: bynder-js-sdk
4+
build: .
5+
command: sh -c "tail -f /dev/null"
6+
volumes:
7+
- .:/app

samples/oauth_client_credentials.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const Bynder = require('../dist/bynder-js-sdk.js');
2+
const configs = require('../secret.json');
3+
4+
5+
const getBynderClient = async() => {
6+
const bynderClient = new Bynder(configs);
7+
const token = await bynderClient.getTokenClientCredentials();
8+
return bynderClient;
9+
}
10+
11+
12+
const getBrands = async (bynder) => {
13+
await bynder.getBrands()
14+
.then((data) => {
15+
console.log(data);
16+
})
17+
.catch((error) => {
18+
console.error(error);
19+
});
20+
}
21+
22+
const getMedia = async(bynder) => {
23+
await bynder.getMediaList({
24+
limit: 5,
25+
page: 1
26+
})
27+
.then((data) => {
28+
console.log('getAssets: ', data, '\n\n');
29+
return bynder.getMediaList();
30+
})
31+
32+
.catch((error) => {
33+
console.error(error)
34+
});
35+
}
36+
37+
const runClientCredentialsSample = async () => {
38+
const bynder = await getBynderClient();
39+
getBrands(bynder);
40+
41+
setTimeout(() =>{
42+
getMedia(bynder);
43+
}, 2000)
44+
}
45+
46+
runClientCredentialsSample();

src/bynder-js-sdk.js

100644100755
+12
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ class Bynder {
234234
});
235235
}
236236

237+
/**
238+
* Gets OAuth2 access token via client credentials grant type
239+
* @return {String} access token
240+
*/
241+
getTokenClientCredentials() {
242+
return this.oauth2.clientCredentials.getToken().then(result => {
243+
const token = this.oauth2.accessToken.create(result);
244+
this.api.token = token;
245+
return token;
246+
});
247+
}
248+
237249
/**
238250
* Get all the smartfilters.
239251
* @see {@link https://bynder.docs.apiary.io/#reference/smartfilters/smartfilters-operations/retrieve-smartfilters|API Call}

0 commit comments

Comments
 (0)