首页 文章

Ember-Simple-Auth与Torii访问用户信息

提问于
浏览
1

我整个星期都在努力让身份验证工作 . 我已经得到了它

  • Ember-CLI

  • Ember-Simple-Auth

  • 鸟居

  • google-oauth2提供商

但是我已经证明从谷歌获取用户信息是不成功的 . 我已经尝试过创建一个torii-adapter,如文档中所述,但它似乎没有被调用

// app/torii-adapters/application.js
export default Ember.Object.extend({
  open: function(authorization){
    console.log('authorization from adapter', authorization);
  }
});

我已经筋疲力尽了google-foo,我正在寻求你的帮助 . 这是一个很好的图书馆组合授权,但是这个案例缺乏文档,当想通时我肯定会回馈 .

谢谢

1 回答

  • 4

    我遇到的问题是Torii默认的google-oauth2提供商不会为您访问此信息,它也使用代码工作流程而不是google API所需的令牌工作流程

    为了解决这个问题,我编写了一个自定义提供程序,它使用对G API的jquery GET请求,然后返回userName和userEmail以在内容下的会话中访问它 .

    我写了一个完整的教程详细说明授权使用谷歌开始完成的一个余烬应用程序here

    //app/torii-providers/google-token.js
    import {configurable} from 'torii/configuration';
    import Oauth2Bearer from 'torii/providers/oauth2-bearer';
    
    var GoogleToken = Oauth2Bearer.extend({
      name: 'google-token',
      baseUrl: 'https://accounts.google.com/o/oauth2/auth',
    
      // additional params that this provider requires
      requiredUrlParams: ['state'],
      optionalUrlParams: ['scope', 'request_visible_actions', 'access_type'],
    
      requestVisibleActions: configurable('requestVisibleActions', ''),
    
      accessType: configurable('accessType', ''),
    
      responseParams: ['token'],
    
      scope: configurable('scope', 'email'),
    
      state: configurable('state', 'STATE'),
    
      redirectUri: configurable('redirectUri',
                                'http://localhost:8000/oauth2callback'),
    
      open: function(){
          var name        = this.get('name'),
              url         = this.buildUrl(),
              redirectUri = this.get('redirectUri'),
              responseParams = this.get('responseParams');
    
          var client_id = this.get('client_id');
    
          return this.get('popup').open(url, responseParams).then(function(authData){
            var missingResponseParams = [];
    
            responseParams.forEach(function(param){
              if (authData[param] === undefined) {
                missingResponseParams.push(param);
              }
            });
    
            if (missingResponseParams.length){
              throw "The response from the provider is missing " +
                    "these required response params: " + responseParams.join(', ');
            }
    
            return $.get("https://www.googleapis.com/plus/v1/people/me", {access_token: authData.token}).then(function(user){
              return {
                userName: user.displayName,
                userEmail: user.emails[0].value,
                provider: name,
                redirectUri: redirectUri
              };
            });
          });
        }
    });
    
    export default GoogleToken;
    

相关问题