Skip to content

Commit

Permalink
New: Select OAuth grant type for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Mantas Lapenas committed Sep 30, 2022
1 parent d45b11b commit 03b6d5a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This a node API wrapper for accessing the Trustpilot APIs. You can learn all abo

## Installation

This module is built using Typescript and Node.js `v8.10.x`.
This module is built using Typescript and Node.js `v12.21.x`.

If you are not using version 4 of Node, you'll have to transpile the code down to ES5 yourself.

Expand Down Expand Up @@ -65,6 +65,24 @@ async run() {
}
```

### Choose OAuth token grant type

For service-to-service communication use 'client_credentials' grant type and API application key, secret combination to obtain an OAuth token. (Default grant type: password)

```ts
import { TrustpilotApi } from "trustpilot";

async run() {
const client = await new TrustpilotApi({
key: 'YOUR-API-KEY',
secret: 'YOUR-SECRET',
granttype: 'client_credentials'
}).authenticate();

// Use client
}
```

### Override API Base URL

The Invitations API methods have a different base URL. To override it, simply pass the `baseUrl`.
Expand Down
5 changes: 3 additions & 2 deletions src/access-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as rp from 'request-promise-native';

import { ITrustpilotApiConfig } from './models';
import { GrantType, ITrustpilotApiConfig } from './models';

export class AccessProvider {
private apiAuthorization: any | undefined;
Expand All @@ -26,12 +26,13 @@ export class AccessProvider {
private createApiTokenRequest() {
const defaultRequest = {
form: {
grant_type: 'password',
grant_type: this.trustpilotApiConfig.grantType || GrantType.password,
password: this.trustpilotApiConfig.password,
username: this.trustpilotApiConfig.username,
},
uri: '/v1/oauth/oauth-business-users-for-applications/accesstoken',
};

return (this.trustpilotApiConfig.tokenRequest) as typeof defaultRequest || defaultRequest;
}

Expand Down
6 changes: 6 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ interface IConfig {
password: string;
tokenRequest: any;
accessToken: string;
grantType: GrantType;
defaultHeaders: IHeaders;
}

export enum GrantType {
password = 'password',
client_credentials = 'client_credentials'
}

export interface ITrustpilotApiConfig extends Partial<IConfig> {}

0 comments on commit 03b6d5a

Please sign in to comment.