我正在尝试使用Torii中的github-oauth2提供程序,但我被如何设置一些回调函数弄糊涂了。我将跟踪我正在使用的代码,以及我对它的理解,希望这可以帮助我准确地指出哪里出了问题。
this.get('torii').open('github-oauth2').then((data) => {
this.transitionTo('dashboard')
})当然,我的config/environment.js中有以下设置
var ENV = {
torii: {
// a 'session' property will be injected on routes and controllers
sessionServiceName: 'session',
providers: {
'github-oauth2': {
apiKey: 'my key',
redirectUri: 'http://127.0.0.1:3000/github_auth'
}
}
},
}redirectUri用于我的Rails服务器。我在我的github应用程序上有相同的redirectUri设置,所以它们是匹配的。
这是我的服务器上的内容。这很可能就是问题所在。我会在最后谈到症状。
def github
client_id = 'my id'
client_secret = 'my secret'
code = params[:code]
@result = HTTParty.post("https://github.com/login/oauth/access_token?client_id=#{client_id}&client_secret=#{client_secret}&code=#{code}")
@access_token = @result.parsed_response.split('&')[0].split('=')[1]
render json: {access_token: @access_token}
end然后,我将该访问令牌打包为json。
这样做的结果是torii弹出窗口转到rails页面:

不幸的是,我希望torii弹出窗口消失,给我的应用程序提供access_token,让代码继续运行并执行我的then代码块中的代码。
我哪里错了?
发布于 2016-05-17 05:16:55
非常感谢Kevin Pfefferle,他帮助我解决了这个问题,并将代码分享到了他的应用程序(gitzoom)中,在那里他实现了一个解决方案。
因此,第一个修复方法是清除我的redirectUri,并在github上将其设置为localhost:4200。这使得应用程序重定向,因此它是一个被重定向到的Ember应用程序。
第二个修复是创建一个自定义的torii提供者
//app/torii-providers/github.js
import Ember from 'ember';
import GitHubOauth2Provider from 'torii/providers/github-oauth2';
export default GitHubOauth2Provider.extend({
ajax: Ember.inject.service(),
fetch(data) {
return data;
},
open() {
return this._super().then((toriiData) => {
const authCode = toriiData.authorizationCode;
const serverUrl = `/github_auth?code=${authCode}`;
return this.get('ajax').request(serverUrl)
.then((data) => {
toriiData.accessToken = data.token;
return toriiData;
});
});
}
});不知道为什么这个then会触发,但是我之前使用的then没有触发。不管怎样,它会获取数据并返回它,然后我之前使用的promise就会正确地获取数据。
this.get('torii').open('github-oauth2').then((data) => {
//do signon stuff with the data here
this.transitionTo('dashboard')
})所以我们开始吧!希望这能帮助其他被困在未来的人们。
https://stackoverflow.com/questions/37262653
复制相似问题