【发布时间】: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: '' }
}
}
我的问题是
-
我是否正确使用令牌? https://developer.spotify.com/documentation/web-api/reference/#/operations/create-playlist
-
我是否走在制作“创建播放列表”功能的正确轨道上?
【问题讨论】:
标签: reactjs post authorization token spotify