簡體   English   中英

使用Firebase登錄的Facebook API調用

[英]Facebook API call with Firebase login

我已登錄綁定到Facebook身份驗證,這全部由Firebase處理。 但是我需要對Facebook 'me/friends/'進行API調用'me/friends/'由於我已經登錄,如何在不另外請求的情況下使用OAuth對象進行調用。

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

你不需要包裝器。 $firebaseAuth() + $http() =簡單的圖形API請求。

Graph API非常易於使用,可以輕松使用Firebase。

確保您已啟用Facebook好友權限,否則您將無法獲得任何數據。

您可以使用$firebaseAuth()登錄並獲取Facebook access_token 該令牌可用於圖譜API,以通過HTTP請求獲取數據。 Angular有一個很好的$http庫來進行這些調用。

不介意我構造代碼的方式,我更喜歡使用Angular樣式指南

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'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM