Google API access_token in Safari using ux_mode: 'redirect'

Viewed 662

Is anyone using ux_mode: 'redirect' and getting a Google access_token in a browser other than Chrome?

When using ux_mode: 'redirect' (to avoid the Google pop-up), I can sign in a user no problem and get an access_token for Google API access. It works for all browsers. However, when using ux_mode: 'redirect', I can only get an id_token unless I'm in Chrome and the sign in has also signed the user into Chrome. (I've asked here on SO if I can get an access_token from an id_token, but no luck there.)

In non-Chrome browsers, I'm getting null for GoogleAuth after the sign in redirects to the redirect_uri.

Here's my config:

const gapiClientConfig: GapiClientConfig = {
  client_id: CLIENT_ID,
  discoveryDocs: [ DISCOVERYDOCS ],
  scope: [ SCOPES ].join(' '),
  ux_mode: 'redirect',
  redirect_uri: environment.redirect_uri
};

This is how I'm initializing GoogleAuth:

    gapi.load('auth2', () => {
           gapi.auth2.init(this.googleApi.getConfig().getClientConfig())
.then((auth: GoogleAuth) => {
                observer.next(auth);
                observer.complete();
              });
          });

This is how I'm signing in:

        auth.signIn().then( function() {
            // this code never runs because of the redirect to another page
            const profile = auth.currentUser.get().getBasicProfile();
            console.log(profile);
          });

I also tried this version of the signIn code but it didn't help.

Promise.resolve(auth.signIn()).then(function() {
    // this never runs
    const profile = auth.currentUser.get().getBasicProfile();
      console.log(profile);
    }).catch(function(error) {
    if (error && error.error === 'popup_blocked_by_browser') {
       // A popup has been blocked by the browser
       } else {
       // some other error
    }
});

Here's how I'm getting the access_token after sign in:

let googleUser: GoogleUser;
this._googleAuth.getAuth(gapiClientConfig)
  .subscribe((auth) => {
      // this only works in Chrome
      googleUser = auth.currentUser.get();
      const profile = googleUser.getBasicProfile();
      console.log('profile redirectLogin:', profile);

      googleToken = googleUser.getAuthResponse(true).access_token;
      console.log('Google user redirectLogin:', googleUser);
});
0 Answers
Related