【问题标题】:First time doing POST request with Spotify's API第一次使用 Spotify 的 API 进行 POST 请求
【发布时间】:2022-01-12 21:38:52
【问题描述】:

我正在尝试制作一个“创建播放列表”按钮。我了解我必须发出发布请求,并且需要令牌才能发送请求。我已经在我的应用程序中设置了令牌授权和其他功能(当前播放状态、显示播放列表),但我一直收到一个错误提示

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

在我的代码中:

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 }));
}

我在上面的代码中定义了令牌:

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: '' }
    }
  }

我的问题是

  1. 我是否正确使用令牌? https://developer.spotify.com/documentation/web-api/reference/#/operations/create-playlist

  2. 我是否走在制作“创建播放列表”功能的正确轨道上?

【问题讨论】:

    标签: reactjs post authorization token spotify


    【解决方案1】:

    看起来在您的componentDidMount() 函数中,您传递的是字符串'token',而不是构造函数中定义的实际变量token

    【讨论】:

    • 我将header 更改为headers'token' 更改为token。现在我有一个新错误,指出“无效的访问令牌”。
    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多