如何获取用户数据(公共配置文件和电子邮件)在离子框架中

时间:2016-09-19 13:12:51

标签: android facebook ionic-framework oauth cordova-plugins

我正致力于Ionic Mobile Application。创建Facebook登录按钮(工作正常)后,我想获取用户数据(我的数据库的公共资料和电子邮件。

我的Index.html

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/angular-sanitize/angular-sanitize.min.js"></script>
<script src="js/ng-cordova.min.js"></script><script src="lib/ng-cordova-oauth/dist/ng-cordova-oauth.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->

<script src="js/app.js"></script>
 <script src="js/services.js"></script>
<script src="js/controllers.js"></script>

Controller.js

.controller('login',['$scope','myService','$location','$state','$timeout',function($scope,myService,$location,$state,$timeout)
{
  $scope.fbLogin = function ($cordovaOauth,$http) {
facebookLogin(window.cordovaOauth, window.http);
console.log(fbLogin);
}
}])

app.js

var app = angular.module('starter', ['ionic', 'starter.controllers','starter.services','ngSanitize',  'ngCordova', 'ngCordovaOauth']);

请告诉我我做错了什么。 有时我得到$ cordovaOauth错误。 模块化不是注入的。

1 个答案:

答案 0 :(得分:1)

function login(){
facebookLogin(window.cordovaOauth, window.http);
}

function facebookLogin($cordovaOauth, $http)
{
$cordovaOauth.facebook("1633195863589792", ["email", "public_profile"], {redirect_uri: "http://localhost/callback"}).then(function(result){
    displayData($http, result.access_token);
},  function(error){
        alert("Error: " + error);
});
}


function displayData($http, access_token)
{
$http.get("https://graph.facebook.com/v2.2/me", {params: {access_token: access_token, fields: "name,gender,location,picture", format: "json" }}).then(function(result) {
    var name = result.data.name;
    var gender = result.data.gender;
    var picture = result.data.picture;


}, function(error) {
    alert("Error: " + error);
});
}