我已将登录绑定到Facebook身份验证,并且这些都是由Firebase处理的。 但是我需要对Facebook进行API调用'me/friends/'
由于我已经登录,我将如何使用OAuth对象进行呼叫而不发出其他请求。使用Firebase登录的Facebook API调用
我正在使用下面的封装Angular连接到Facebook。 https://github.com/ccoenraets/OpenFB
我已将登录绑定到Facebook身份验证,并且这些都是由Firebase处理的。 但是我需要对Facebook进行API调用'me/friends/'
由于我已经登录,我将如何使用OAuth对象进行呼叫而不发出其他请求。使用Firebase登录的Facebook API调用
我正在使用下面的封装Angular连接到Facebook。 https://github.com/ccoenraets/OpenFB
你不需要包装。$firebaseAuth()
+ $http()
=易图表API请求。
Graph API非常易于使用,并且可以轻松使用Firebase。
确保您已启用Facebook好友权限,否则您将不会收到任何数据。您可以使用$firebaseAuth()
登录并获取Facebook access_token
。该令牌可用于图形API以通过HTTP请求获取数据。 Angular有一个很好的$http
库来进行这些调用。
不介意我构造代码的方式,我更喜欢使用Angular styleguide。
angular.module('app', ['firebase'])
.constant('FirebaseUrl', 'https://<my-firebase-app>.firebaseio.com/')
.constant('FacebookAppId', '<app-id>')
.service('RootRef', ['FirebaseUrl', Firebase])
.factory('Auth', Auth)
.factory('Friends', Friends)
.controller('MainCtrl', MainCtrl);
function Friends($http, RootRef, $q, FacebookAppId) {
function getFriends() {
// get the currently logged in user (may be null)
var user = RootRef.getAuth();
var deferred = $q.defer();
var token = null;
var endpoint = "https://graph.facebook.com/me/friends?access_token="
// if there is no logged in user, call reject
// with the error and return the promise
if (!user) {
deferred.reject('error');
return deferred.promise;
} else {
// There is a user, get the token
token = user.facebook.accessToken;
// append the token onto the endpoint
endpoint = endpoint + token;
}
// Make the http call
$http.get(endpoint)
.then(function(data) {
deferred.resolve(data);
})
.catch(function(error) {
deferred.reject(error);
});
// return the promise
return deferred.promise;
}
return {
get: getFriends
};
}
Friends.$inject = ['$http', 'RootRef', '$q', 'FacebookAppId'];
function Auth($firebaseAuth, RootRef) {
return $firebaseAuth(RootRef);
}
Auth.$inject = ['FirebaseAuth', 'RootRef'];
function MainCtrl($scope, Friends) {
$scope.login = function login() {
Auth.$authWithOAuthPopup('facebook').then(function(authData) {
console.log(authData, 'logged in!');
});
};
$scope.getFriends = function getFriends() {
Friends.get()
.then(function(result) {
console.log(result.data);
});
};
}
MainCtrl.$inject = ['$scope', 'Friends'];
也许这已经过时了。目前'getAuth()'不返回'user.facebook'属性。我面临同样的问题。 –
@matheusrufca,我想现在(10/2016)你会使用'firebase.auth()。currentUser.providerData'。请参阅'auth' [文档页面](https://firebase.google.com/docs/auth/web/manage-users)。 – AlanP
@AlanP我一直在寻找存储在用户上的fb访问令牌。目前,似乎获得fb acess标记的唯一方法是使用'$ signInWithPopup'方法回调。 '$ getAuth'和'$ onAuth'不提供Facebook数据。 –
您可以在'authData.accessToken'中找到Facebook令牌。请参阅http://stackoverflow.com/questions/16688613/firebase-facebook-front-end-front-end-query/16697338#comment24081501_16697338 –
让我知道我的回答是否适合您。如果是这样,请将其标记为已接受。清楚地保持未答复的队列是很好的。 –