在 Spotify API 授权期间获取 HTTP 415 Unsupported Media

我想使用此处提到的“客户端凭据流”访问 Spotify Web API 。我尝试使用此处提到的一些教程来访问它,但无法遵循它,因为它使用了节点中的请求库,该库现在已被弃用,所以我尝试在它的位置使用 axios 并在浏览器中不断收到状态代码 415 错误安慰。我正在尝试使用此 API 创建一个辅助项目以进行练习,因为我是初学者。我已经使用 NodeJS 和普通浏览器控制台完成了这项工作(我在这里使用了浏览器控制台的代码)并且我安装了 axios 和 express 库(使用节点时)。


const client_id = '**redacted**';

const client_secret = '**redacted**';


const res = axios.post('https://accounts.spotify.com/api/token', {

  headers: {

    'Authorization': 'Basic ' + btoa(`${client_id}` + ':' + `${client_secret}`),

  },

  form: {

    grant_type: 'client_credentials'

  }

});


console.log(res)

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>AXIOS DOC</title>

</head>

<body>

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script src="script2.js"></script>

</body>

</html>


慕运维8079593
浏览 257回答 2
2回答

芜湖不芜

Spotify 的 API 要求您将 POST 请求的正文编码为application/x-www-form-urlencoded.&nbsp;要使用 axios 执行此操作,您需要使用其他库或serialize()自己制作函数(我使用了此答案serialize(obj)中的函数)。<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>AXIOS DOC</title></head><body>&nbsp; &nbsp; <script src="https://unpkg.com/axios/dist/axios.min.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; const client_id = '**redacted**';&nbsp; &nbsp; &nbsp; &nbsp; const client_secret = '**redacted**';&nbsp; &nbsp; &nbsp; &nbsp; const serialize = function(obj) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var str = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var p in obj) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (obj.hasOwnProperty(p)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return str.join("&");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; axios&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .post('https://accounts.spotify.com/api/token',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; serialize({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grant_type: 'client_credentials'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }), {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Authorization': 'Basic ' + btoa(client_id + ':' + client_secret),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then(res => console.log(res.data.access_token))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .catch(err => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script></body></html>另一种解决方案是简单地使用自动序列化数据的请求库。此外,请注意使用客户端凭据授权类型发出请求。通过这种方式,您可以公开您client_secret的应用程序并让其他人可以模拟您的应用程序。客户端凭据授予类型应仅在服务器端使用,您可以确保您client_secret不会被暴露。如果您想在客户端安全地对用户进行身份验证,请使用带有 PKCE(更好)或隐式授权类型的授权代码。

HUX布斯

我偶然发现了一个最近使用 axios 发出 POST 请求而不是请求库的代码片段。我刚刚尝试过,并且能够收到访问令牌!如果您有任何问题,请告诉我。这是链接。一定要安装 qs 包。代码看起来像这样:// Headers object.const headers = {&nbsp;headers: {&nbsp; &nbsp; Accept: 'application/json',&nbsp; &nbsp; 'Content-Type': 'application/x-www-form-urlencoded',&nbsp; },&nbsp; auth: {&nbsp; &nbsp; username: client_id,&nbsp; &nbsp; password: client_secret,&nbsp; },};// Data object.const data = {&nbsp; grant_type: 'client_credentials',};// Make the request using the URL, query string, data, and headers.const res = axios.post('https://accounts.spotify.com/api/token', qs.stringify(data), headers);// Retrieve the access token from the response.const access_token = res.data.access_token;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript