【发布时间】:2019-05-09 09:25:23
【问题描述】:
我正在使用隐式授权流程将 Spotify API 与 AngularJS 一起使用,但我无法获得我的 access_token。
我以这种方式实现了隐式授权流程:
const hash = window.location.hash
.substring(1)
.split('&')
.reduce(function (initial, item) {
if (item) {
var parts = item.split('=');
initial[parts[0]] = decodeURIComponent(parts[1]);
}
return initial;
}, {});
window.location.hash = '';
// Set token
let _token = hash.access_token;
const authEndpoint = 'https://accounts.spotify.com/authorize';
// Replace with your app's client ID, redirect URI and desired scopes
const clientId = 'bcb7a7...13727c';
const redirectUri = 'http://localhost/~mathieu/';
const scopes = [
'user-read-birthdate',
'user-read-email',
'user-read-private'
];
// If there is no token, redirect to Spotify authorization
if (!_token) {
window.location = `${authEndpoint}?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scopes.join('%20')}&response_type=token`;
}
当我继续访问 url 时,我很好地重定向到了 Spotify 身份验证,但是当我连接时,浏览器进入一个重定向循环:spotify 将我重定向到 localhost/~mathieu,它将我重定向到 spotify 等...
我想在 Spotify 重定向我之后,我的脚本无法获取令牌,所以我再次被重定向,但我找不到解决方案。
请帮帮我
【问题讨论】:
-
为什么这个标签是 angular?
-
你说得对,angularjs 标签更好,谢谢哦,也许你问为什么是 angularjs ?因为我想在 angularJS 项目中使用 Spotify API,而且由于我不知道问题的根源,所以我更愿意提供所有信息。
-
你可以使用 Angular 的 OAuth 库,而不是自己管理它,也许。
-
我遵循了 Spotify api 文档,你的意思是我可以更简单地获得 access_token ?
-
不不,我只是因为角度标签而点击了这个问题,然后我觉得你在 angularJS 中这样做。
标签: javascript angularjs api spotify access-token