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

NodeJS example not working with Typescript #112

Open
schuettc opened this issue Apr 11, 2024 · 0 comments
Open

NodeJS example not working with Typescript #112

schuettc opened this issue Apr 11, 2024 · 0 comments

Comments

@schuettc
Copy link

Attempting to create a NodeJS app using SpotifyApi.withClientCredentials. When creating with JavaScript, the example here works just fine.

However, when attempting using Typescript, it fails.

import { SpotifyApi } from '@spotify/web-api-ts-sdk';

console.log('Searching Spotify for The Beatles...');

const SPOTIFY_CLIENT_SECRET = process.env.SPOTIFY_CLIENT_ID;
const SPOTIFY_CLIENT_ID = process.env.SPOTIFY_CLIENT_SECRET;

if (!SPOTIFY_CLIENT_ID || !SPOTIFY_CLIENT_SECRET) {
  throw new Error('Missing Spotify credentials');
}

const spotifyApi = SpotifyApi.withClientCredentials(
  SPOTIFY_CLIENT_ID,
  SPOTIFY_CLIENT_SECRET,
);

export const handler = async () => {
  const items = await spotifyApi.search('The Beatles', ['artist']);

  console.table(
    items.artists.items.map((item) => ({
      name: item.name,
      followers: item.followers.total,
      popularity: item.popularity,
    })),
  );

  return {
    statusCode: 200,
  };
};

handler();
{
  "name": "example_node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.3"
  },
  "devDependencies": {
    "@types/node": "^20.9.0",
    "typescript": "^5.2.2"
  }
}

Using the same credentials, it fails with this error:

$ node dist/index.js
Searching Spotify for The Beatles...
/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/auth/ClientCredentialsStrategy.js:59
            throw new Error("Failed to get access token.");
                  ^

Error: Failed to get access token.
    at ClientCredentialsStrategy.getTokenFromApi (/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/auth/ClientCredentialsStrategy.js:59:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/auth/ClientCredentialsStrategy.js:24:27
    at async InMemoryCachingStrategy.getOrCreate (/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/caching/GenericCache.js:27:30)
    at async ClientCredentialsStrategy.getOrCreateAccessToken (/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/auth/ClientCredentialsStrategy.js:23:23)
    at async SpotifyApi.makeRequest (/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/SpotifyApi.js:75:33)
    at async SearchEndpoints.getRequest (/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/endpoints/EndpointsBase.js:9:16)
    at async SearchEndpoints.execute (/Users/courtschuett/Documents/GitHub/schuettc/spotify-discover-weekly-saver/node_modules/@spotify/web-api-ts-sdk/dist/cjs/endpoints/SearchEndpoints.js:10:16)

Node.js v20.5.1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant