4.3.2 • Published 2 years ago

spotify-auth-token v4.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Spotify Auth Token

npm GitHub Workflow Status (branch) codecov

A simple and lightweight implementation of Spotify's Authorization Code Flow.

  • CLI and programmatic use
  • Customizable
  • Gets both short-lived and refresh tokens

Implements the authorization code flow according to the Spotify docs.

Use it for any application that interacts with the Spotify API and needs to run independently. The generated refresh token can be used to request new short-lived tokens on the fly.

This helper was mainly developed to simplify my Spotify history scrobbler.

Example result

The short-lived access_token expires after 1 hour. The refresh_token can be used to request new short-lived tokens whenever needed.

{
  "access_token": "BQCmqcTBlB2uVBEzLhykD9dYi",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "AQB-B_Kg3Kn5nL3ByG1hQA",
  "scope": "user-library-read",
  "date_obtained": "Sat, 12 Feb 2022 10:25:41 GMT"
}

CLI Usage

The simplest way to get a token is via npx - no installation required.

npx spotify-auth-token@latest --clientId f40c6b --clientSecret 0199f38a

Optional arguments example:

npx spotify-auth-token@latest --clientId f40c6b --clientSecret 0199f38a --port 8000 --scopes "user-library-read"

With short flags:

npx spotify-auth-token@latest -ci f40c6b -cs 0199f38a -p 8000 -s "user-library-read user-top-read"

Options

Flag(required?) Description
-ci | --clientId✅ Spotify client id
-cs | --clientSecret✅ Spotify client secret
-p | --port❌ Port for localhost redirect url. Default: 3000
-s | --scopesSpotify auth scopes, separated by a space. Default: 'user-read-email'
-o | --outDir❌ Custom output directory relative to the current directory
-f | --fileName❌ Custom file name for the token

Programmatic

Installation

yarn add -D spotify-auth-token

or

npm install spotify-auth-token --save-dev

Options

The options and defaults are the same as for the CLI plus noEmit. If noEmit is set to true, the token will not be saved to the file system.

type Options = {
  clientId: string;
  clientSecret: string;
  port?: number;
  scopes?: string;
  fileName?: string;
  outDir?: string;
  noEmit?: boolean;
};

Examples

  • CommonJS
const { authorize } = require('spotify-auth-token/dist/authorize');

const token = await authorize({
  clientId: 'clientId',
  clientSecret: 'clientSecret',
  port: 3000,
  scopes: 'user-read-email user-top-read',
  noEmit: true,
});
  • ES Modules
import authorize from 'spotify-auth-token';

const token = await authorize({
  clientId: 'clientId',
  clientSecret: 'clientSecret',
  port: 3000,
  scopes: 'user-read-email user-top-read',
});
  • TypeScript
import authorize, { UserConfig } from 'spotify-auth-token';

const config: UserConfig = {
  clientId: 'clientId',
  clientSecret: 'clientSecret',
  port: 3000,
  scopes: 'user-read-email user-top-read',
};

const token = await authorize(config);
4.3.2

2 years ago

4.3.1

2 years ago

4.3.0

2 years ago

4.2.2

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.0

2 years ago

4.0.0

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago