在ember

时间:2015-11-23 11:32:00

标签: javascript ajax facebook ember.js

我是ember和JavaScript的新手。我设计了一个ember应用程序,并在其中集成了fb登录。现在我必须将fb登录的access_token发送到app后端(post request)并获取新生成的令牌后端(django)。我还需要存储新令牌,以便将来调用后端。

发布请求回复

{
  "token": "b0fb466a6eac3ad87",
  "signup_done": true
}

控制器

import Ember from 'ember'; 
/* global FB */

export default Ember.Controller.extend({ 
getLoginStatus: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    FB.getLoginStatus(function(response) {
      if (response.status) {
        resolve(response.status);
      } else {
        reject();
      }
    });
  });
}, 
getAccessToken: function() {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    FB.login(function(response) {
      if (response.authResponse) {
        console.log("accesstoken>>>>>>>>>"+response.authResponse.accessToken);
        resolve(response.authResponse.accessToken);
      } else {
        reject(response);
      }
    }, { scope: 'email,user_photos,user_friends' });
  });
},

 authenticate: function(accessToken) {
    console.log("accesstoken<<<<<<<<<<<             "+accessToken);
      Ember.$.ajax({
        url: 'http://example.com/api/',
        data:{
          access_token: accessToken,
          provider:'facebook'
        },
        type: 'post',
        success:function(data){
          console.log('data>>>'+this.get(data));
        }
      });
  },

 testAPI:function() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
    });
},



   actions: {
    fblogin: function() {
      var self = this;
      this.getLoginStatus().then(function() {
        self.getAccessToken().then(function(accessToken) {
          self.authenticate(accesstoken)
            self.testAPI();
            console.log('success')
          }, function(reason) {
            console.log('failure', reason);
          });
        });

    },
    carousel:function (result) {
          var photos=result.photoThumbs
          this.set('model.photos',photos)
          console.log(this.get("model.results"));
          },
  }

});

我能够对后端服务器进行ajax调用并获得响应。但是不确定如何保存新令牌以备将来使用。

0 个答案:

没有答案