gpt4 book ai didi

javascript - 使用 Firebase 登录的 Facebook API 调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:44:03 25 4
gpt4 key购买 nike

我已将登录绑定(bind)到 Facebook 身份验证,这全部由 Firebase 处理。但是我需要对 Facebook 'me/friends/' 进行 API 调用由于我已经登录,我如何在不发出另一个请求的情况下使用 OAuth 对象进行调用。

我正在使用以下 Angular 包装器连接到 Facebook。 https://github.com/ccoenraets/OpenFB

最佳答案

您不需要包装器。 $firebaseAuth() + $http() = easy Graph API 请求。

Graph API非常易于使用,并且可以轻松地与 Firebase 一起使用。

确保您启用了 Facebook 好友权限,否则您将无法取回任何数据。

您可以使用 $firebaseAuth() 登录并获取 Facebook access_token。该 token 可用于 Graph 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'];

关于javascript - 使用 Firebase 登录的 Facebook API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com