Can't authorize with spotify using javascript AJAX request [ CORS ] [duplicate]

[亡魂溺海] 提交于 2019-12-25 17:46:12

问题


I'm trying to authorize with spotify by making an AJAX get request in javascript. Right now I have this function get called when a button on my html webpage gets pressed:

var accessToken = null;
var client_id = '5a576333cfb1417fbffbfa3931b00478'
var redirect_uri = 'http://127.0.0.1:8000/popularify'
var scopes = 'playlist-modify-public'
var response_type_token = 'code'
var state = '123123123'
var auth_url = 'https://accounts.spotify.com/authorize?client_id=5a576333cfb1417fbffbfa3931b00478&redirect_uri=http://127.0.0.1:8000/popularify&response_type=token&show_dialog=true&scope=playlist-modify-public'

    async function spotifyAuth() {

        console.log("spotifyAuth() called")

        // If access token has been assigned in the past and is not expired, no request required. 
        if (sessionStorage.getItem("accessToken") !== null && sessionStorage.getItem("tokenTimeStamp") !== null && upTokenTime < tokenExpireSec) {
            console.log("token already exists and hasnt expired yet")

        } else {
            console.log("Token expired or never found, getting new token.");

            $.ajax({
                url: auth_url,
                type: 'GET',
                contentType: 'application/json',
                //headers: { "X-Requested-With": "XMLHttpRequest" },
                data: {
                    client_id: client_id,
                    redirect_uri: redirect_uri,
                    scope: scopes,
                    response_type: response_type_token,
                    state: state,
                }
            }).done(function callback(response) {
                // Redirect user to home page 
                console.log("COULD THIS BE A SUCCESS?");
                $(location).attr('href', this.url);

            }).fail(function (error) {
                // Since we cannot modify the server, we will always fail.
                console.log("ERROR HAPPENED: " + error.status);
                console.log(this.url);
                $(location).attr('href', this.url);
            });

            

        }
    }

Currently when I press the button, I get these messages in console:

Right afterwards it opens up this spotify url in the page:

https://accounts.spotify.com/en/authorize?client_id=5a576333cfb1417fbffbfa3931b00478&client_id=5a576333cfb1417fbffbfa3931b00478&redirect_uri=http:%2F%2F127.0.0.1:8000%2Fpopularify&redirect_uri=http:%2F%2F127.0.0.1:8000%2Fpopularify&response_type=token&response_type=code&show_dialog=true&scope=playlist-modify-public&scope=playlist-modify-public&state=123123123

and it all looks correct, I can click the spotifyt UI sign in button, but once I do it just goes to this URL ( https://accounts.spotify.com/authorize/accept) which is just a blank page with the text Illegal_redirect_uri

Is there something wrong with my spotify url? could the url encoded redirect url be causing issues? Or is it a CORS issue? I had been trying to use a cors proxy but it wasnt working so I want to go back to this, since the spotify sign in site opens correctly, but it isnt getting redirected back to my page (which is currently set to http://127.0.0.1:8000/popularify for testing, and i have that url set in my spotify app dashboard as a redirect url)

来源:https://stackoverflow.com/questions/59420413/cant-authorize-with-spotify-using-javascript-ajax-request-cors

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