0
votes

I am trying to make a "create a playlist" button. I understand that I must make a post request and a token is required to send the request. I have already setup token authorization and other features in my app work (current playback state, displaying playlists) but I keep getting an error that states

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

in my code:

componentDidMount() {
    const requestOptions = {
        method: 'POST',
        header: { 
                   'Content-Type': 'application/json',
                   'Authorization': 'Bearer' + 'token'
                   
         },

        body: JSON.stringify({ title: 'CreatePlaylistTest' })
    };
    fetch('https://api.spotify.com/v1/users/12141627583/playlists', requestOptions)
        .then(response => response.json())
        .then(data => this.setState({ postId: data.id }));
}

I defined token in my code above it:

import React, { Component } from 'react';
import './App.css';

import SpotifyWebApi from 'spotify-web-api-js';
const spotifyApi = new SpotifyWebApi();

class App extends Component {
  constructor(){
    super();
    const params = this.getHashParams();
    const token = params.access_token;
    if (token) {
      spotifyApi.setAccessToken(token);
    }
    this.state = {
      loggedIn: token ? true : false,
      nowPlaying: { name: 'Not Checked', albumArt: '' },
      myPlaylists: { playlists: '' }
    }
  }

My question is

  1. Am I using token correctly? https://developer.spotify.com/documentation/web-api/reference/#/operations/create-playlist

  2. Am I on the right track towards making a "create playlist" feature?