“415 Error” when querying Spotify for tokens

安稳与你 提交于 2020-01-11 08:21:25

问题


I've been trying to recreate the spotify oauth connection in MeteorJS. I've gotten as far as requesting the access and refresh tokens, but I keep getting a 415 error now. Here is the relevant code:

 var results = HTTP.post(
                'https://accounts.spotify.com/api/token',
                {
                    data: {
                        code: code,
                        redirect_uri: redirectURI,
                        grant_type: 'authorization_code',
                        client_id: clientID,
                        client_secret: clientSecret
                    },
                    headers: {
                        'Content-Type':'application/json'
                    }
                }
            );

I can't seem to find any other good documentation of the problem and the code in this demo:

https://github.com/spotify/web-api-auth-examples/tree/master/authorization_code

works perfectly.


回答1:


I had a similar problem (but in Java). The analogous solution was

                    headers: {
                        'Content-Type':'application/x-www-form-urlencoded'
                    }



回答2:


You need to use params instead of data when sending the JSON object. Related question: Unsupported grant type error when requesting access_token on Spotify API with Meteor HTTP




回答3:


If youre doing this clientside its not working because you're not allowed to post to another domain from the client side because of the same origin policy.

If this is server-side I'd recommend using a pre-existing spotify api npm module instead of writing your own requests. There are plenty of spotify api implementations on npmjs.org.

Use arunoda's npm package for integrating npm packages in your meteor application



来源:https://stackoverflow.com/questions/25432678/415-error-when-querying-spotify-for-tokens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!