使用Spotify弹出窗口实现登录 [英] Implement Log In With Spotify Popup
本文介绍了使用Spotify弹出窗口实现登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
推荐答案
这就是Spotify开发者网站的做法:
- 打开一个弹出窗口,打开/api/Authorize。一旦用户允许该应用程序,它会将他重定向到回调页面。
- 在回调页面,使用返回的授权码(GET参数
code
),通过POST请求/api/Token(查看文档)生成访问/刷新令牌。这应该在服务器端完成,因为它需要发送客户端ID和客户端密钥。 - 将访问/刷新令牌存储在本地存储中并关闭弹出窗口。
- 检测关闭事件,从本地存储获取令牌并将其用于API。
示例
登录页面:
// Open the auth popup
var spotifyLoginWindow = window.open('https://accounts.spotify.com/authorize?client_id=REPLACE_ME&redirect_uri=REPLACE_ME&response_type=code');
// Close event
spotifyLoginWindow.onbeforeunload = function() {
var accessToken = localStorage.getItem('sp-accessToken');
var refreshToken = localStorage.getItem('sp-refreshToken');
// use the code to get an access token (as described in the documentation)
};
回调页面:
// Assuming here that the server has called /api/token
// and has rendered the access/refresh tokens in the document
var accessToken = "xxx";
var refreshToken = "xxx";
/////////////////////////
// Store the tokens
localStorage.setItem("sp-accessToken", accessToken);
localStorage.setItem("sp-refreshToken", refreshToken);
// Close the popup
window.close();
这篇关于使用Spotify弹出窗口实现登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文