google / google-api-javascript-client

Google APIs Client Library for browser JavaScript, aka gapi.
Apache License 2.0
3.18k stars 1.05k forks source link

Accessing Gapi on an Ionic (Cordova) app with cordova-plugin-googleplus #551

Open JilReg opened 5 years ago

JilReg commented 5 years ago

Hey @TMSCH,

I have read all your posts in #401, in #304 and in #360. Sadly I'm still did not figure out how to solve my problem (accessing Google APIs trough Gapi on a Cordova device after restarting the app.)

I would appreciate every helping advice! 🤗

What I would like to achieve

1.) I would like to let users sign in with Google account into an Ionic / Cordova app running on iOS and on Android via Firebase. 2.) I would like afterwards to access the users emails in Gmail via Gapi. And being able to do so after closing and opening the app.

What I have achieved

export class GoogleLoginComponent implements OnInit {
  user: Observable<firebase.User>;
  private course: any;

  constructor(
    private afAuth: AngularFireAuth,
    private gplus: GooglePlus,
    private platform: Platform
  ) {
    this.user = this.afAuth.authState;
    this.initGapiClient();
  }

  async nativeGoogleLogin(): Promise<firebase.auth.UserCredential> {
    try {
      const gplusUser = await this.gplus.login({
        webClientId: firebaseConfig.clientId,
        offline: true,
        scopes:
          'profile ' +
          'email ' +
          'https://mail.google.com/ ' +
          'https://www.googleapis.com/auth/gmail.modify ' +
          'https://www.googleapis.com/auth/gmail.readonly'
      });

      return await this.afAuth.auth.signInWithCredential(
        firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken)
      );
    } catch (err) {
      console.log(err);
    }
  }

  async getGmailMessagesList() {
    const messages = await gapi.client.gmail.users.messages.list({
      userId: 'me'
    });

}

To constructor([...]):

      gapi.load('client');

To nativeGoogleLogin():

      gapi.client.setToken({ access_token: gplusUser.accessToken });
      gapi.client.load('gmail', 'v1');

What I figured out / think

yuzhakovvv commented 4 years ago

@JilReg did you manage to run it on Cordova?

In Google Cloud Console for used Client ID it's required to whitelist Origins from where requests will come. But Cordova can set only localhost:8080 Origin which is not acceptable for Google as far as I know when they verify you app

yuzhakovvv commented 4 years ago

Actually I can use Google API CORS to resolve my question above