【问题标题】:Spotify API : can't have my access token with implicit grant flowSpotify API:无法通过隐式授权流获得我的访问令牌
【发布时间】: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


【解决方案1】:

如果您在 Angular 中使用路由器,则路由器可能会在此代码从中获取值之前剥离 URL 的哈希值。您需要确保此代码在您的角度路由器代码运行之前运行。或者,您可以连接到角度路由器并在那里检索哈希。 This question 似乎表明这是可能的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2018-01-08
    • 2020-10-09
    • 1970-01-01
    • 2013-12-04
    • 2021-10-23
    相关资源
    最近更新 更多