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

[FIX] auth-module: Refactor ClientCredentialsStrategy constructor and request handling #97

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { SpotifyApi } from '@spotify/web-api-ts-sdk';

// Choose one of the following:
const sdk = SpotifyApi.withUserAuthorization("client-id", "https://localhost:3000", ["scope1", "scope2"]);
const sdk = SpotifyApi.withClientCredentials("client-id", "secret", ["scope1", "scope2"]);
const sdk = SpotifyApi.withClientCredentials("client-id", "secret");
```

Each of these factory methods will return a `SpotifyApi` instance, which you can use to make requests to the Spotify Web API.
Expand Down
4 changes: 2 additions & 2 deletions src/SpotifyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export class SpotifyApi {
return new SpotifyApi(strategy, config);
}

public static withClientCredentials(clientId: string, clientSecret: string, scopes: string[] = [], config?: SdkOptions): SpotifyApi {
const strategy = new ClientCredentialsStrategy(clientId, clientSecret, scopes);
public static withClientCredentials(clientId: string, clientSecret: string, config?: SdkOptions): SpotifyApi {//change

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably be asked to remove the //change comment from this line.

const strategy = new ClientCredentialsStrategy(clientId, clientSecret);
return new SpotifyApi(strategy, config);
}

Expand Down
6 changes: 2 additions & 4 deletions src/auth/ClientCredentialsStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export default class ClientCredentialsStrategy implements IAuthStrategy {

constructor(
private clientId: string,
private clientSecret: string,
private scopes: string[] = []
private clientSecret: string
) {
}

Expand Down Expand Up @@ -46,8 +45,7 @@ export default class ClientCredentialsStrategy implements IAuthStrategy {

private async getTokenFromApi(): Promise<AccessToken> {
const options = {
grant_type: 'client_credentials',
scope: this.scopes.join(' ')
grant_type: 'client_credentials'
} as any;

const bodyAsString = Object.keys(options).map(key => key + '=' + options[key]).join('&');
Expand Down