【问题标题】:Can't figure out Spotify's Authorization in a React app在 React 应用程序中无法弄清楚 Spotify 的授权
【发布时间】:2018-04-29 17:25:03
【问题描述】:

我想要实现的目标:用户单击 React 页面上的按钮,被重定向到 Spotify 进行授权,返回到我的页面并且我的应用收到访问令牌。

我认为我需要使用他们授权文档中的implicit grant flow。我正在使用 jQuery 发送 GET 请求:

$.ajax({
  url: urlToFetch,
  type: 'GET',
  success: data => {
    // what do I put here?
  }
})

我不知道在成功函数中添加什么,将用户重定向到他们的登录页面,然后使用访问令牌将他们重定向回来?

对不起,如果这是一个愚蠢的问题 - 我是初学者,但我无法弄清楚。

【问题讨论】:

  • 您没有在 React 中执行请求是否有原因?
  • @Colin 如果您指的是以下内容,我已经尝试过:fetch(urlToFetch).then(response => { }, 'json') - 但我仍然不知道在大括号内放什么。我的问题是我不明白要编写什么函数来将用户重定向到登录页面并返回。
  • 你应该研究一下 react-router 之类的东西。或者,您可以使用 window.location.href = '/whatever'; 导航到 URL。

标签: jquery ajax api spotify


【解决方案1】:

如果您只需将用户重定向到登录页面,您只需设置 window.location 的值即可。

看这个例子here

var url = 'https://accounts.spotify.com/authorize';
url += '?response_type=token';
url += '&client_id=' + encodeURIComponent(client_id);
url += '&scope=' + encodeURIComponent(scope);
url += '&redirect_uri=' + encodeURIComponent(redirect_uri);
url += '&state=' + encodeURIComponent(state);

window.location = url;

这可能不足以满足您的需求,因此您可能需要查看本教程:

https://medium.com/@jonnykalambay/now-playing-using-spotifys-awesome-api-with-react-7db8173a7b13

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多