React / Redux + Soundcloud API

罗宾·维鲁奇

更新

在这里查看工作示例:Favesound-Redux

直播:http//www.favesound.de/

教程:http//www.robinwieruch.de/the-soundcloud-client-in-react-redux

问题

最近,我发现了Sound Redux并从中得到启发,它展示了如何在React + Redux应用程序中使用Soundcloud API。Soundcloud API要求您设置callback.html页面。Sound Redux应用程序通过从Go Lang服务器提供callback.html来解决此问题。我不想为此使用任何服务器端技术。这就是为什么我想知道是否可以将callback.html用作react组件。我的设置已经弹出了Soundcloud的身份验证模式,但是输入我的凭据后什么也没有发生,并且模式变为空白。

在我的根组件中,设置回调组件和应用程序组件的路由。

const routes = <Route component={App}>
  <Route path="/callback" component={Callback} />
  <Route path="/app" component={SoundContainer} />
</Route>;

ReactDOM.render(
  <Provider store={store}>
     <Router>{routes}</Router>
  </Provider>,
  document.getElementById('app')
);

当我从SoundContainer内部触发身份验证请求操作时,操作创建者如下所示:

export function auth() {
  return dispatch => {
    SC.initialize({
      client_id: MY_CLIENT_ID,
      redirect_uri:`${window.location.protocol}//${window.location.host}/#/callback`
    });

    SC.connect(function (response) {
      console.log('connect'); // This does not appear in the console
      SC.get('/me', function(data) {
        console.log(data);
        dispatch(doAuth(data));
      })
    });
  }
}

就像我说的那样,在模态中输入我的凭据后,模态变为空白,什么也没有发生。

当我输出时,${window.location.protocol}//${window.location.host}/#/callback它与我的Soundcloud帐户中的重定向Uri相同:http://localhost:8080/#/callback

我的回调组件如下所示:

export default React.createClass({
  render: function() {
    return <html>
      <head>
        <title>Callback</title>
      </head>
      <body onload="window.setTimeout(opener.SC.connectCallback, 1);">
        <p>
          This page should close soon
        </p>
      </body>
    </html>
  }
});
危险的

首先,好主意。

当您使用哈希时,回调只会是一个片段,而不是完整的页面。回调代码不应包含<html>在其中。您可以尝试一下吗?

export default React.createClass({
  componentDidMount:function(){
    window.setTimeout(opener.SC.connectCallback, 1);
  },
  render: function() {
    return (
      <div>
          <p>
            This page should close soon
          </p>
      </div>
    );
  }
});

更新

好的,所以oAuth2不喜欢返回回调URL中的片段,这可能就是代码无法正常工作的原因。但是,您可以让您的快递服务器在回调路由上重新提供水疗中心。该技术在react-router createBrowserHistory文档中详细记录。因此,回调URL将为您的spa页面提供服务,并且登录将完成。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Phonegap和SoundCloud API

来自分类Dev

Soundcloud API 401

来自分类Dev

React-Redux-Hooks API

来自分类Dev

使用API访问Soundcloud图表

来自分类Dev

Soundcloud API停止返回曲目

来自分类Dev

Soundcloud和API速率限制

来自分类Dev

Soundcloud API Stream 不同的歌曲

来自分类Dev

使用REST API进行React + Redux?

来自分类Dev

Redux React从API创建初始状态

来自分类Dev

在React / Redux上使用API的正确方法

来自分类Dev

使用Soundcloud API播放特定歌曲

来自分类Dev

SoundCloud API是否有配额限制?

来自分类Dev

Soundcloud API ReferenceError:未定义$(JavaScript)

来自分类Dev

在GoLang中寻找Soundcloud API的入门资源

来自分类Dev

SoundCloud Track List API返回504

来自分类Dev

Soundcloud Api-歌手跟踪

来自分类Dev

获取相关曲目列表HTTP API SoundCloud

来自分类Dev

soundcloud api php添加关注者

来自分类Dev

Soundcloud API的无效javascript / JSONP响应

来自分类Dev

如何使用Soundcloud API下载轨道?

来自分类Dev

Soundcloud API /流端点产生401错误

来自分类Dev

使用Facebook帐户连接到SoundCloud API

来自分类Dev

SoundCloud API是否有配额限制?

来自分类Dev

Chrome不会加载jQuery或SoundCloud API

来自分类Dev

绕过SoundCloud API的SCLoginViewController,而是使用openURL

来自分类Dev

获取相关曲目列表HTTP API SoundCloud

来自分类Dev

如何使用Soundcloud API下载轨道?

来自分类Dev

通过API网址获取Soundcloud的完整URL

来自分类Dev

Soundcloud API作为后端音乐广播服务