发布POST请求Spotify API(Issue with POST Request To Spotify API)
我正在研究一个Web应用程序,您可以在其中搜索Spotify库,将歌曲添加到播放列表中,然后将该播放列表添加到Spotify帐户。 除了通过POST请求将播放列表保存到Spotify API之外,一切似乎都在进行中。 它给了我一个“400错误请求”错误。 我可能只是错过了一些小的,任何想法? 请求方法:
async savePlaylist(name, trackURIs) { if(accessToken === undefined) { this.getAccessToken(); } if (name === 'New Playlist' || name === undefined || trackURIs === undefined) { return; } else { let userAccessToken = this.getAccessToken(); let headers = {Authorization: `Bearer ${userAccessToken}`}; let userId = await this.findUserId(); let playlistID; fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, { method: 'POST', headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": 'application/json' }, body: { name: name } }).then(response => {return response.json()} ).then(playlist => { playlistID = playlist.id; }); } },
如果您需要查看我正在从事的分支的回购,可以在这里找到。
编辑:可能需要API端点文档
I'm working on a web application where you can search the Spotify Library, add songs to a playlist, then add that playlist to your Spotify Account. Everything seems to be working besides saving the Playlist through a POST request to the Spotify API. It gives me a "400 Bad Request" Error. I probably just missed something small, any ideas? The request method:
async savePlaylist(name, trackURIs) { if(accessToken === undefined) { this.getAccessToken(); } if (name === 'New Playlist' || name === undefined || trackURIs === undefined) { return; } else { let userAccessToken = this.getAccessToken(); let headers = {Authorization: `Bearer ${userAccessToken}`}; let userId = await this.findUserId(); let playlistID; fetch(`https://api.spotify.com/v1/users/${userId}/playlists`, { method: 'POST', headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": 'application/json' }, body: { name: name } }).then(response => {return response.json()} ).then(playlist => { playlistID = playlist.id; }); } },
If you need to take a look at the repo for the branch I'm working on, it can be found here.
EDIT: Might need the API Endpoint Documentation
原文:https://stackoverflow.com/questions/48604037
最满意答案
由于您在公司设置中使用代理,并且该代理可能会插入用户的bash配置文件中。 你可以做3件事
更新〜/ .bash_profile或〜/ .bashrc
从您的配置文件中删除代理,您将不需要代理
取消设置变量
在呼叫之前,您可以取消设置变量
unset http_proxy unset https_proxy curl -i -X POST -H 'content-type: text/json' -d @post.json \ http://127.0.0.1:18082/youtu/openliveapi/livedetectfour
为呼叫清空变量
您可以为该卷曲调用设置变量
http_proxy= https_proxy= curl -i -X POST -H 'content-type: text/json' -d @post.json \ http://127.0.0.1:18082/youtu/openliveapi/livedetectfour
Since you are using proxy in your corporate setup and the proxy might be insert in your user's bash profile. You can do 3 things
Update ~/.bash_profile or ~/.bashrc
Remove the proxy from your profile and you won't need the proxy
Unset the variables
You can unset the variables before the call
unset http_proxy unset https_proxy curl -i -X POST -H 'content-type: text/json' -d @post.json \ http://127.0.0.1:18082/youtu/openliveapi/livedetectfour
Blank the variables for the call
You can set the variables just for that one curl call
http_proxy= https_proxy= curl -i -X POST -H 'content-type: text/json' -d @post.json \ http://127.0.0.1:18082/youtu/openliveapi/livedetectfour
相关问答
更多-
Linux cURL XML文件POST - 如何检查错误/成功?(Linux cURL XML file POST - how to check for error/success?)[2022-08-08]
您可以使用-w %{http_code}获取操作的响应状态 curl -s -o out.txt -w %{http_code} http://www.example.com/ 在此示例中, -s表示静默模式, -o out.txt表示将响应(通常为html)保存到文件中。 对于上面的命令,当它成功时你输出200 。 You can get the response status of your operation using -w %{http_code} curl -s -o out.txt -w % ... -
Linux curl post请求和错误417,但sudo成功?(Linux curl post request and Error 417 but sudo success?)[2023-10-10]
由于您在公司设置中使用代理,并且该代理可能会插入用户的bash配置文件中。 你可以做3件事 更新〜/ .bash_profile或〜/ .bashrc 从您的配置文件中删除代理,您将不需要代理 取消设置变量 在呼叫之前,您可以取消设置变量 unset http_proxy unset https_proxy curl -i -X POST -H 'content-type: text/json' -d @post.json \ http://127.0.0.1:18082/youtu/openliveapi ... -
您必须指定发布的数据是JSON类型 将此行添加到您的curl代码中 curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/json")); You have to specify that the posted data is of type JSON add this line to your curl code curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-T ...
-
我通读了GitHub文档,并在底部描述了如何获取图形输出: C&C / Boxer API提供了一个入口点,用于生成给定文本的DRG的PNG图像: $ CANDCAPI / DRG URL接受与管道相同的GET参数并返回原始PNG文件。 基于此,您的GET URL应如下所示: http://gingerbeard.alwaysdata.net/candcapi/proxy.php/drg?semantics=fol 使用“相同”的GET参数意味着在问号之后传递给pipeline任何内容也可以传递给drg ...
-
关于上面的对话,让我写一个答案。 如果您在rails中看到一个表单,它将具有如下所述的行 现在,如果在ApplicationController你有这行protect_from_forgery那么它总是会期望authenticity_token ,如果它不可用,它将抛出你日志中提到的错误,因此我建议删除该行 ...
-
有几种选择。 您可以通过exec()函数输出json内容后安装CRON作业。 返回json数据后,从您的页面发出AJAX请求。 There are several options for you. You can install CRON job after outputting json content via exec() function. Make AJAX request from your page after json data was returned.
-
cURL无法验证正在使用的证书的真实性,因为在本地数据库中找不到签名授权机构的证书。 这可能是使用自签名证书的症状。 您应该做的是将签名权限的证书添加到/etc/ssl/certs/ca-certificates.crt 。 你可以做的是使用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); ,这将禁用失败的检查。 你真的应该使用第一个选项。 cURL is not able to verify the authenticity of the certifica ...
-
@RequestBody只需要一个JSON数组(如果用于注释List 。 你必须做两件事之一: 使用单个private List
ids字段创建表示POST数据的类型。 更改控制器方法签名以使用该类型而不是List 。 将您的JSON POST更改为: curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '[1,2]' 两种解决方案都应 ... -
curl POST请求无效(curl POST request is not working)[2023-11-20]
您已将Content-Type设置为application/x-www-form-urlencoded因此您必须发送表单URL编码数据: curl -k -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \ -H "Accept: application/json, text/plain, */*" \ -H "Authorization: OAuth oauth_consumer_key=2 ... -
尝试添加这个: curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); Try adding this: curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));