【发布时间】:2019-12-30 07:45:18
【问题描述】:
我正在使用 Spotify web api 使用 angular 制作 web 应用程序。
虽然我能够对用户进行身份验证并获取播放列表/曲目,甚至创建播放列表等,但在用户同意我的应用程序的范围后,我仍然无法从 redirect_url 获取 access_token。
目前我可以通过使用下面的代码来完成上述操作......但这是一种非常糟糕且低效的方式。
Service.ts:
// ASK USER TO GRANT PERMISSION FOR THE FOLLOWING SCOPES
Login(showDialog = true) {
const state = uuid();
localStorage.setItem('xsrf-token', state);
const params = {
response_type: 'token',
client_id: "ec540590bb6449a181edhjdjsdjkdshasbdjk",
redirect_uri: "http://localhost:4200/selectionMenu",
scope:
'user-top-read%20user-read-private%20playlist-read-private%20user-library-modify%20playlist-modify-public%20user-follow-read%20user-read-playback-state%20user-modify-playback-state%20user-read-recently-played%20user-read-currently-playing%20user-follow-modify%20playlist-modify-private%20playlist-read-collaborative%20user-library-read%20user-read-email',
state,
show_dialog: `${showDialog}`
}
const redirectUrl = `https://accounts.spotify.com/authorize?${qs.stringify(params)}`;
window.location.href = redirectUrl;
}
在此之后,我可以显示以下屏幕:
在用户活动后[如果他点击同意],spotify 服务将重定向到以下网址:
http://localhost:4200/selectionMenu
但会将 access_token 附加到上面的 url,所以我的页面将重定向到的最终 url 如下:
http://localhost:4200/selectionMenu#access_token=BQASUoH7jashdjakshdashvday63hvsjadjhqBEWmVi9bKNg7EBF7Dt2dCvSNWLlQ3zUOp6hGuLfvJ9QH4PXRLTz4AajPDyBW7SV9lXhFb_IF25qb7hheUsvyg7pX-zD-TsOtbRCb7ohF86wrjWGFRXuugT-mnWESBMCRVSxTi34nOcG4VpvM-Ew9F0kl1vq5zdsSRSnCvZwmsQNGngDVpj2rsS2nQG0EIXdqTmSut0CdZFFobz5k9ojpbw971ye5dl1cXnHlc&token_type=Bearer&expires_in=3600&state=59418612-e4cb-46bb-90af-d01f7a2c790a
画面如下:
要从上面的 url 中提取 access_token,我目前正在执行以下操作:
在 Component.ts 中:
currentWindowUrl() {
const pathname = new URL(window.location.href).pathname;
const redirectURL = window.location.href.split('#');
usableURL = redirectURL[1].split('&');
this.getTokens();
}
getTokens() {
const accessToken = usableURL[0].split('=')[1];
const tokenType = usableURL[1].split('=')[1];
const refreshTime = usableURL[2].split('=')[1];
const state = usableURL[3].split('=')[1];
// pass accessToken to service and store it.
this.spotifyAuthorizeService.saveAccessToken(accessToken);
}
我基本上是在使用 window.location.href 并将其拆分,直到我得到 access_token,这听起来很蹩脚。
我的问题
->我如何获取 Spotify 附加到 redirect_url 的访问令牌,以更好的方式并将其存储在 localStorage[不适合 xhr 攻击],任何其他更好的方式可以存储它以便我可以将它用于我的应用程序中的所有其他页面,并在每 60 分钟 [令牌到期时间] 后刷新一次。
->有没有办法不显示url中#之后的部分,即access_token,这样我就不会放弃它。
Angular-Auth-OIDC
我遵循了文档并提出了以下配置:
assets/auth.clientConfiguration.json:
{
"stsServer": "https://accounts.spotify.com/authorize",
"redirect_url": "http://localhost:4200/selectionMenu",
"client_id": "ec5405hgasfdhasfdashdgdcbd27987aef6",
"response_type": "token",
"scope": "user-top-read%20user-read-private%20playlist-read-private%20user-library-modify%20playlist-modify-public%20user-follow-read%20user-read-playback-state%20user-modify-playback-state%20user-read-recently-played%20user-read-currently-playing%20user-follow-modify%20playlist-modify-private%20playlist-read-collaborative%20user-library-read%20user-read-email",
"post_logout_redirect_uri": "https://localhost:4200/",
"start_checksession": true,
"silent_renew": true,
"silent_renew_url": "https://accounts.spotify.com/authorize",
"post_login_route": "/selectionMenu",
"forbidden_route": "/forbidden",
"unauthorized_route": "/unauthorized",
"log_console_warning_active": true,
"log_console_debug_active": true,
"max_id_token_iat_offset_allowed_in_seconds": 10
}
我正在为应用程序提供以下错误:
当我点击登录时,应用程序重定向到以下网址:
https://accounts.spotify.com/authorize/.well-known/openid-configuration
最后出现 405 错误。
我的理解
我知道 url 配置错误,我不确定在 stsServer 中指定什么 url,我猜它是 spotify 的 url 因为它提供令牌服务但我不确定我在 json 文件中配置的 url 是否正确。
对此的任何见解都会有所帮助。
谢谢!
【问题讨论】:
标签: javascript angular typescript spotify