1
votes

I currently have a Facebook app written with app that uses firebase to authenticate and login to my app. I am getting a access token with the firebase auth. I wanted to use this token to make graph api calls like

FB.api(
     '/me/albums',
     'GET',
     {},
     function(response) {
        // Insert your code here
        console.log(response);
        console.log(token);
     }
  );

I am following the documentation on firebase to make the authentication. The user is successfully created on firebase the user has given permission for photo access. I just am not able to figure out how to use the token to makes calls to Facebook's graph api.

 var provider = new firebase.auth.FacebookAuthProvider();
 provider.addScope('user_photos');
 provider.addScope('user_friends');
 firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Facebook Access Token. You can use it to access the Facebook API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // ...
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
});

EDIT When I make the call after the redirect I get the following error

"An active access token must be used to query information about the current user."
2

2 Answers

3
votes

You could do what @luschn said or you could also make a simple http request with facebook api.

$.get(
     "https://graph.facebook.com/me",
     {
        'fields'       : fields,
        'access_token' : token
     },
     function(response) {
     //enter code here
     }
)

You can get the fields from facebook's graph api and access token is the one you got from firebase.

2
votes

While working with the JS SDK, you donĀ“t have to deal with the User Token after authorization. I am not sure how it works with Firebase, but i assume that you have to add the Token on your own if you want to use the JS SDK after login with Firebase:

FB.api(
    '/me/albums', {fields: '...', access_token: token}, function(response) {
        console.log(response);
    }
);

Also, make sure the Access Token is valid and includes the user_photos permission. You can debug it here: https://developers.facebook.com/tools/debug/accesstoken/

You can also try using the Fetch API instead of the JS SDK to make API calls with the Token from Firebase: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API