0

I want to use my existing authentication and be able to use that same authentication to perform a get request to the Google Directory API. Here's my current code:

login() {
    this.firebaseRef = new Firebase('https://xxx.firebaseio.com');
    this.firebaseRef.authWithOAuthPopup("google", (error, authData) => {
        if (error) {
            console.log("Login Failed!", error);
        } else {
            console.log("Authenticated successfully with payload:", authData);
        }
    }, {
            scope: "https://www.googleapis.com/auth/admin.directory.user.readonly"
    });
}

getData() {
    // TO-DO
    // Recognise existing OAuth and perform a GET request to
    // https://www.googleapis.com/admin/directory/v1/users?domain=nunoarruda.com
}
nunoarruda
  • 2,679
  • 5
  • 26
  • 50

2 Answers2

1

You could leverage the getAuth method on the firebaseRef instance. Something like that:

getData() {
  var authData = this.firebaseRef.getData();
  var provider = authData.provider;
  // In your case provider contains "google"
}

See this documentation: https://www.firebase.com/docs/web/api/firebase/getauth.html.

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
1

I've found the solution. I need to use the current access token in the http request headers for the GET request.

import {Http, Headers} from 'angular2/http';

getData() {
    // get access token
    let authData = this.firebaseRef.getAuth();
    let oauthToken = authData.google.accessToken;
    console.log(oauthToken);

    // set authorization on request headers
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + oauthToken);

    // api request
    this.http.get('https://www.googleapis.com/admin/directory/v1/users?domain=nunoarruda.com',{headers: headers}).subscribe((res) => {
        console.log(res.json());
    });
}
nunoarruda
  • 2,679
  • 5
  • 26
  • 50