Skip to content

Commit

Permalink
API-2196 support oauth client credentials js sdk (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahongbynder authored Jan 28, 2025
1 parent 6888169 commit 06c4723
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:11

RUN mkdir /app
WORKDIR /app

COPY . /app/

RUN npm install
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: run-docker
run-docker:
docker-compose up -d

.PHONY: stop-docker
stop-docker:
docker-compose down

# make executeSdkSample sample-file-name=oauth_client_credentials.js
.PHONY: executeSdkSample
executeSdkSample:
docker-compose exec bynder-js-sdk node /app/samples/$(sample-file-name)
49 changes: 49 additions & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ const bynder = new Bynder({
});
```

#### OAuth2 Client Credentials

OAuth application within Bynder needs client credentials grant type. Bynder object can be instantiated without redirectUri provided:

```js
const bynder = new Bynder({
baseURL: "http://api-url.bynder.io/api/",
clientId: "<your OAuth2 client id>",
clientSecret: "<your OAuth2 client secret>"
});
```

To get a token via client credentials, make a call to `getTokenClientCredentials()`:

```js
const bynder = new Bynder({
baseURL: "http://api-url.bynder.io/api/",
clientId: "<your OAuth2 client id>",
clientSecret: "<your OAuth2 client secret>"
});
const token = await bynder.getTokenClientCredentials();
```

Sample call can be found within `samples/oauth_client_credentials.js`.

#### Making requests

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

- `makeAuthorizationURL()`
- `getToken()`
- `getTokenClientCredentials()`

### Media

Expand Down Expand Up @@ -195,3 +221,26 @@ are the steps necessary to prepare your environment:
- `gulp doc` - Run JSDoc to create a 'doc' folder with automatically generated documentation for the source code.
- `gulp webserver` - Deploy a web server from the root folder at
`localhost:8080` to run the html samples (in order to avoid CORS problems).

## Docker Setup


JavaScript files can be executed within a Docker container with corresponding dependencies installed.

### Initial Setup

From the root directory create a `secret.json` file.

```json
{
"baseURL": "http://api-url.bynder.io/api/",
"clientId": "<your OAuth2 client id>",
"clientSecret": "<your OAuth2 client secret>"
}
```

Run the command `make run-docker` to start up Docker container.

With the container running, execute the command:

`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.
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
bynder-js-sdk:
container_name: bynder-js-sdk
build: .
command: sh -c "tail -f /dev/null"
volumes:
- .:/app
46 changes: 46 additions & 0 deletions samples/oauth_client_credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const Bynder = require('../dist/bynder-js-sdk.js');
const configs = require('../secret.json');


const getBynderClient = async() => {
const bynderClient = new Bynder(configs);
const token = await bynderClient.getTokenClientCredentials();
return bynderClient;
}


const getBrands = async (bynder) => {
await bynder.getBrands()
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}

const getMedia = async(bynder) => {
await bynder.getMediaList({
limit: 5,
page: 1
})
.then((data) => {
console.log('getAssets: ', data, '\n\n');
return bynder.getMediaList();
})

.catch((error) => {
console.error(error)
});
}

const runClientCredentialsSample = async () => {
const bynder = await getBynderClient();
getBrands(bynder);

setTimeout(() =>{
getMedia(bynder);
}, 2000)
}

runClientCredentialsSample();
12 changes: 12 additions & 0 deletions src/bynder-js-sdk.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ class Bynder {
});
}

/**
* Gets OAuth2 access token via client credentials grant type
* @return {String} access token
*/
getTokenClientCredentials() {
return this.oauth2.clientCredentials.getToken().then(result => {
const token = this.oauth2.accessToken.create(result);
this.api.token = token;
return token;
});
}

/**
* Get all the smartfilters.
* @see {@link https://bynder.docs.apiary.io/#reference/smartfilters/smartfilters-operations/retrieve-smartfilters|API Call}
Expand Down

0 comments on commit 06c4723

Please sign in to comment.