首页 \ 问答 \ 发布POST请求Spotify API(Issue with POST Request To Spotify API)

发布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
更新时间:2023-04-05 12:04

最满意答案

由于您在公司设置中使用代理,并且该代理可能会插入用户的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

相关问答

更多
  • 您可以使用-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 % ...
  • 由于您在公司设置中使用代理,并且该代理可能会插入用户的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]' 两种解决方案都应 ...
  • 您已将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:'));

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)