expo: accessToken and idToken are undefined when calling GoogleSignIn.signInAsync()

šŸ› Bug Report

When calling the new GoogleSignIn.signInAsync() method, accessToken and idToken are undefined. To test this I created a new android standalone build expo build:android and installed on my device. The sign in is successful but the accessToken and idToken are undefined. I need this to be able to sign into firebase.

I followed these docs: https://docs.expo.io/versions/v33.0.0/sdk/google-sign-in/

Seems to work ok when I was using the old Expo.Google way to sign in: https://docs.expo.io/versions/v33.0.0/sdk/google/

Environment

Expo CLI 2.20.5 environment info: System: OS: Windows 10 Binaries: npm: 6.2.0 - C:\Program Files\nodejs\npm.CMD

Testing this on Android, not iOS.

Steps to Reproduce

I have the following code:

  try {
    // Docs show that clientID is for IOS only. I'm testing on Android only.
    await GoogleSignIn.initAsync();
  } catch ({ message }) {
    alert('GoogleSignIn.initAsync(): ' + message);
  }
  try {
    await GoogleSignIn.askForPlayServicesAsync();
    const { type, user } = await GoogleSignIn.signInAsync();
    if (type === 'success') {
      // I get a success here but user.idToken is null

      //  Sign into Firebase with the credential from the Google user.
      const credential = firebase.auth.GoogleAuthProvider.credential(user.idToken, user.accessToken);
      firebase
        .auth()
        .signInAndRetrieveDataWithCredential(credential)
        .then(function(result) {
          alert('Firebase sign in success');
        })
        .catch(function(error) {
          console.error('Error with Firebase sign in ', error.message);
        });
    }
  } catch ({ message }) {
    alert('login: Error:' + message);
  }

Expected Behavior

accessToken and idToken should be returned.

Actual Behavior

accessToken and idToken are undefined.

Reproducible Demo

Can’t create snack as GoogleSignIn cannot be used with the Expo client, only standalone.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22 (5 by maintainers)

Most upvoted comments

Yeah that’s what I did (this API can’t be used in the Expo client, so standalone is the only option)

Steps for successful googleSignIn behavior-

  • expo init newproject
  • set android.package in app.json to com.example.newproject
  • create new firebase project
  • ā€œGet started by adding Firebase to your appā€ -> click the android btn
  • enter your exact android.package
  • download config file, add it to your project
  • in your app.json, set expo.android.googleServicesFile to the relative path of your google-services.json file
  • build project with expo build:android, once it’s completed run expo fetch:android:hashes and copy Google Certificate Fingerprint
  • go to your project’s settings in firebase, and ā€œAdd Fingerprintā€ using the copied Google Certificate Fingerprint from previous step

I got firebase to work now too. I was stupidly using the stringified version of user.auth.idToken and user.auth.accessToken. Correct code:

try {
  await GoogleSignIn.initAsync();
} catch ({ message }) {
  alert('GoogleSignIn.initAsync(): ' + message);
}

try {
  await GoogleSignIn.askForPlayServicesAsync();
  const { type, user } = await GoogleSignIn.signInAsync();
  if (type === 'success') {
    alert(JSON.stringify(user)); // I now have access to user.auth.idToken

    const credential = firebase.auth.GoogleAuthProvider.credential(
      user.auth.idToken,
      user.auth.accessToken,
    );

    firebase
      .auth()
      .signInAndRetrieveDataWithCredential(credential)
      .then(function(result) {
        alert('Firebase sign in success');
      })
      .catch(function(error) {
        console.error('Error with Firebase sign in ', error.message);
      });
  } else {
    alert(type);
  }
} catch (error) {
  alert('login: Error:\n' + error.message + '\n' + JSON.stringify(error));
}

This is happening to me in the android studio emulator, but it works for ios.

My app.json looks like:

"expo": {
  "android": {
      "package": "com.appname",
      "googleServicesFile": "./android/app/google-services.json"
    }
}

my firebase project setting has the Package name as: com.appname, the google-services.json file is in the correct location…

Not sure where the issue is.

EDIT: I also added the debug SHA-1 certificate displayed when executing this command from the terminal: keytool -list -v -alias androiddebugkey -keystore ~/.android/debug.keystore