无法使用javascript AJAX请求通过Spotify授权[CORS]

时间:2019-12-20 05:48:26

标签: javascript jquery html cors spotify

我正在尝试通过在JavaScript中发出AJAX获取请求来授权Spotify。 现在,当我的html网页上的按钮被按下时,我将调用此函数:

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);
            });

            

        }
    }

当前,当我按下按钮时,我会在控制台中收到以下消息: enter image description here

此后,它将在页面中打开此Spotify网址:

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

一切看起来正确,我可以单击spotifyt UI登录按钮,但是一旦执行该操作,就转到该URL(https://accounts.spotify.com/authorize/accept),该URL只是一个空白页面,文本为Illegal_redirect_uri

我的Spotify网址有问题吗?网址编码的重定向网址可能会引起问题吗?还是CORS问题?我一直在尝试使用cors代理,但该代理无法正常工作,因此我想回到此页面,因为Spotify登录网站可以正确打开,但是它并没有重定向回到我的页面(当前设置为{{3} }进行测试,并且在我的Spotify应用信息中心中将该网址设置为重定向网址)

0 个答案:

没有答案