首页 > 解决方案 > 从 javascript [spotify auth] 中的弹出窗口获取访问令牌 url

问题描述

我正在尝试在纯 javascript 中创建一个 spotify 身份验证流程,以便用户可以登录,然后我可以为他们的帐户添加一个新的播放列表。根据我阅读的说明,我使用了一个身份验证弹出窗口,一旦他们登录,就会在 URL 中包含访问令牌。我现在有一个弹出窗口,用户可以使用它进行身份验证,一旦他们这样做,它将在 url 中有访问令牌。

我需要从弹出窗口中获取 url 并将其保存为全局变量,但我无法弄清楚如何在 javascript 中执行此操作。

https://codepen.io/martin-barker/pen/YzPwXaz

我的 codepen 打开一个弹出窗口let popup = window.open(,我可以在弹出窗口中运行一个函数来检测用户何时成功进行身份验证并且 url 更改?在这种情况下,我想保存用于解析的 url 并关闭我的弹出窗口

我的javascript代码如下:

async function spotifyAuth() {
let result = spotifyLogin()
}

//open popup
function spotifyLogin() {
console.log("inside spotifyLogin, opening popup")

let popup = window.open(`https://accounts.spotify.com/authorize?client_id=5a576333cfb1417fbffbfa3931b00478&response_type=token&redirect_uri=https://codepen.io/martin-barker/pen/YzPwXaz&show_dialog=true&scope=playlist-modify-public`, 'Login with Spotify', 'width=800,height=600')

}

//get url from popup and parse access token????
window.spotifyCallback = (payload) => {
console.log("inside window? ") //this line never appears in console
popup.close()
fetch('https://api.spotify.com/v1/me', {
headers: {
'Authorization': `Bearer ${payload}`
}
}).then(response => {
return response.json()
}).then(data => {
// do something with data
})
}

标签: javascriptspotify

解决方案


这是我在 JavaScript 中的做法。像你提到的全局变量:

var access_token = null;

例如,我的网址看起来像这样: https://...home.jsp#access_token=BQAXe5JQOV_xZmAukmw6G430lreF......rQByzZMcOIF2q2aszujN0wzV7pIxA4viMbQD6s&token_type=Bearer&expires_in=3600&state=vURQeVAoZqwYm4dC

在 Spotify 将用户重定向到您在仪表板上指定的 uri 之后,我解析包含访问令牌的哈希的 url,如下所示:

var hash = window.location.hash.substring(1);
var accessString = hash.indexOf("&");

/* 13 because that bypasses 'access_token' string */
access_token = hash.substring(13, accessString);
console.log("Access Token: " + access_token);

输出是:

Access Token: BQAXe5JQOV_xZmAukmw6G430lreF...........rQByzZMcOIF2q2aszujN0wzV7pIxA4viMbQD6s

我将此访问令牌保存在 sessionStorage 中,以防用户离开页面并且 url 不包含 access_token。我假设这是隐式授权流程,因为您想使用纯 JavaScript。只要确保在它们过期后每小时重新获取一个访问令牌。

附录

我可以向您展示如何获取令牌并在示例中使用它。

我在 .html 页面上有一个按钮,一旦单击该按钮,就会在名为

测试.js

function implicitGrantFlow() {

/* 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) {
        var timeLeft = (tokenExpireSec - upTokenTime);
        console.log("Token still valid: " + Math.floor(timeLeft / 60) + " minutes left.");

        /* Navigate to the home page. */
        $(location).attr('href', "home.jsp");
} else {
    console.log("Token expired or never found, getting new token.");
    $.ajax({
        url: auth_url,
        type: 'GET',
        contentType: 'application/json',
        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);
    });
}

我正在做的是检查我存储在 sessionStorage 中的 access_token 信息是否为空。我使用 Epoch 以来的时间来生成令牌的创建时间以及理想情况下应该到期的时间。如果满足这些参数,那么我不会再拨打电话。

否则,我拨打电话以获取访问令牌,成功后会将我重定向到我的 uri,正如我在之前的文章中提到的那样(你会看到我在 .fail 部分中有重定向。这是因为我没有有权在我的学校服务器上设置设置以绕过 CORS 相关问题,即使我创建的重定向 url 很好,也会阻止我的呼叫成功。)。

然后,当我的白名单 uri 被加载(重定向到我的主页)时,我会使用我的<body>标签。

主页.jsp

<body onload="getAccessToken()">

在我的标签中,一旦页面加载,我就会调用此函数。这将调用函数 getAccessTokens()。

/**
 * The bread and butter to calling the API. This function will be called once the
 * user is redirected to the home page on success and without rejecting the terms
 * we are demanding. Once through, this function parses the url for the access token
 * and then stores it to be used later or when navigating away from the home page.
 */
function getAccessToken() {

    access_token = sessionStorage.getItem("accessToken");

    if (access_token === null) {
        if (window.location.hash) {
            console.log('Getting Access Token');

            var hash = window.location.hash.substring(1);
            var accessString = hash.indexOf("&");

            /* 13 because that bypasses 'access_token' string */
            access_token = hash.substring(13, accessString);
            console.log("Access Token: " + access_token);

            /* If first visit or regaining token, store it in session. */    
            if (typeof(Storage) !== "undefined") {
                /* Store the access token */
                sessionStorage.setItem("accessToken", access_token); // store token.

                /* To see if we need a new token later. */
                sessionStorage.setItem("tokenTimeStamp", secondsSinceEpoch);

                /* Token expire time */
                sessionStorage.setItem("tokenExpireStamp", secondsSinceEpoch + 3600);
                console.log("Access Token Time Stamp: "
                + sessionStorage.getItem("tokenTimeStamp")
                + " seconds\nOR: " + dateNowMS + "\nToken expires at: "
                + sessionStorage.getItem("tokenExpireStamp"));
            } else {
                alert("Your browser does not support web storage...\nPlease try another browser.");
            }
        } else {
            console.log('URL has no hash; no access token');
        }
    } else if (upTokenTime >= tokenExpireSec) {
        console.log("Getting a new acess token...Redirecting");

        /* Remove session vars so we dont have to check in implicitGrantFlow */
        sessionStorage.clear();

        $(location).attr('href', 'index.html'); // Get another access token, redirect back.

    } else {
        var timeLeft = (tokenExpireSec - upTokenTime);
        console.log("Token still valid: " + Math.floor(timeLeft / 60) + " minutes left.");
    }

在这里,一旦我从 url 获得访问令牌,我就会将令牌存储在会话存储中。我使用了我之前文章中提到的过程,但这里是完整的 JavaScript。如果评论后仍然不清楚,请告诉我。

现在我们已经获得并存储了访问令牌,我们现在可以进行 api 调用。以下是我的做法(并且一直在使用 qQuery,这是获取用户热门曲目的示例)。

示例 api 调用

/**
 * Function will get the user's top tracks depending on the limit and offset
 * specified in addition to the time_range specified in JSON format.
 * @param time_range short/medium/long range the specifies how long ago.
 * @param offset Where the indexing of top tracks starts.
 * @param limit How many tracks at a time we can fetch (50 max.)
 */
function getUserTopTracks(time_range, offset, limit) {

$.get({
    url: 'https://api.spotify.com/v1/me/top/tracks',
    headers: {
        'Authorization': 'Bearer ' + access_token,
    },
    data: {
        limit: limit, // This is how many tracks to show (50 max @ a time).
        offset: offset, // 0 = top of list, increase to get more tracks.
        time_range: time_range // short/medium/long_term time ranges.
    },
    success: function (response) {

        /* Get the items from the response (The limit) tracks. */
        res = JSON.parse(JSON.stringify(response.items));

        /* Get all the track details in the json */
        for (i = 0; i < res.length; i++) {
            console.log("Track: " + res[i]);
        }
    },
    fail: function () {
        console.log("getUserTopTracks(): api call failed!");
    }
});

参数 time_range 被指定为“long_term”以获取用户从一开始的热门曲目(阅读更多关于 Spotify 的文档以获取更多信息)除了偏移量为 0 以从开头开始并且限制等于 50,因为这是最大值每次调用获取。

成功时,我有我的响应变量“响应”,然后我希望解析的根从“项目”部分开始,以使解析更容易(您不必这样做,您只需使用 response.xxx.items。 xxx)。然后我将响应打印到控制台。

这是您可以做的基本事情,您决定如何处理或存储数据取决于您。我不是专家,我上个学期才开始学习 Web 编程,我正在做的很多实践可能是错误的或不正确的。


推荐阅读