Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get refresh token for google api using Firebase authentication

From firebase's documentation

firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the 
Google 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;
// ...
});

Is there anyway I can get the refresh token for google api using firebase authentication. I couldn't find anything about this problem in Firebase's documentation. I am also aware that the User object also contains a refreshToken. Can I use that refreshToken from firebase to generate a new access_token for google api ?

like image 437
Huy Tran Avatar asked Apr 19 '18 19:04

Huy Tran


1 Answers

Firebase Auth is currently focused on AuthN and not AuthZ. They do not manage OAuth tokens on sign in. All OAuth refresh tokens are discarded and only the initial OAuth access token is returned. If you need a Google refresh token, or a Google access token continuously, consider using GApi library to get a Google ID token/access token and then sign in with that to Firebase.

function onGoogleSignIn(googleUser) {
  var googleIdToken = googleUser.getAuthResponse().id_token;
  firebase.auth().signInWithCredential(
    firebase.auth.GoogleAuthProvider.credential(googleIdToken));
}

You will always have the ability to get a Google OAuth access token from the Google sign in library that way.

like image 102
bojeil Avatar answered Oct 01 '22 22:10

bojeil