简体   繁体   English

Spotify API:不能具有隐式授予流的访问令牌

[英]Spotify API : can't have my access token with implicit grant flow

i'm using the Implicit grant flow to use Spotify API with AngularJS, but i can't have my access_token. 我正在使用隐式授予流来将Spotify API与AngularJS一起使用,但是我无法使用access_token。

I implemented implicit grant flow this way : 我以这种方式实现了隐式授予流:

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`;
}

When i go on the url, i am well redirected to the Spotify authentification, but when i connect, the browser enters in a loop of redirection : spotify redirects me to localhost/~mathieu which redirects me to spotify etc... 当我进入url时,我可以很好地重定向到Spotify身份验证,但是当我连接时,浏览器进入重定向循环:spotify将我重定向到localhost /〜mathieu,这会将我重定向到spotify等。

I suppose that after Spotify redirects me, my script can't get the token so i am redirected again, but i can't find a solution. 我想在Spotify重定向我之后,我的脚本无法获得令牌,因此我又被重定向了,但是我找不到解决方案。

Please, help me 请帮我

If you're using a router in angular, it's likely that the router strips the hash of the URL before this code can grab the value out of it. 如果您使用的是有角度的路由器,则很可能是路由器在此代码可以从中获取值之前就剥离了URL的哈希值。 You need to make sure that this code runs before your angular router code runs. 您需要确保此代码在您的角度路由器代码运行之前运行。 Alternatively you can hook into the angular router and retrieve the hash there. 另外,您也可以插入有角路由器并在其中检索哈希值。 This question seems to suggest that's possible. 这个问题似乎表明这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM