4

In Firebase 2 I was able to get facebook accessToken from anywhere after login by this way, just using the firebase reference, eg:

firebase.getAuth().facebook.accessToken

Now, how can I get that in version 3 (web)?

Note, I need it outside of the Promise signInWithPopup

Thanks

user2976753
  • 935
  • 11
  • 24

4 Answers4

6

I just found this on documentation... Why? :(

Since Firebase Authentication no longer persists the access token, your application will have to do so itself, if needed.

user2976753
  • 935
  • 11
  • 24
  • It looks like this is related to inconsistencies in the auth apis for some of their providers – ForrestLyman Dec 17 '16 at 16:04
  • The documentation is pretty clear now: https://firebase.google.com/support/guides/firebase-web#get_the_access_token "With the Firebase.com Authentication API, you can easily use the provider's access token to call out to the provider's API and get additional information. This access token is still available, but only immediately after the sign-in action has completed." – Cameron Taggart Mar 29 '18 at 22:14
5

Try this:

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;
})
Marwelln
  • 28,492
  • 21
  • 93
  • 117
chanat
  • 173
  • 1
  • 9
  • 1
    That's inside the login, what I mean is outside "after login", I'm using AWS cognito and I need that `accessToken` in other side... so I could I get it from other class/method? – user2976753 May 22 '16 at 17:36
  • 2
    @user2976753 Did you ever find the answer? I'm looking for a solution as well. – hybrid9 Jan 18 '17 at 14:33
  • But saving and managing the token yourself is problematic as the Google one (= gapi token) expires after 60 minutes. With new Firebase one would seem to need to fire the login routine (popup) all over again to get to the Google token... This is the most relevant thread I've found: http://stackoverflow.com/questions/41387535/how-to-refresh-google-accesstoken-in-firebase-askfirebase – Vitali Kniazeu May 07 '17 at 20:06
  • @VitaliKniazeu But OP is asking about fb login, not google... – AlxVallejo Apr 18 '20 at 23:46
1

Possible answer is to unlink "auth.currentUser.unlink(provider)" the provider and re link "auth.currentUser.linkWithPopup(provider)" each time you need to get the access token.

0

The easiest way to get the user access token is to use the FB API itself.

Simply follow this guide on how to include it and then get the token the following way:

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    var accessToken = response.authResponse.accessToken;
  } 
} );
Orlandster
  • 4,706
  • 2
  • 30
  • 45