Get Google access_token with Angular?

Viewed 74

I have an Angular 4 app that is running on Firebase. I am using the sign in with email and password feature with Firebase. Which is working perfectly.

login.component.ts

onSubmit(formData) {
    if(formData.valid) {
        console.log(formData.value);
        this.afAuth.auth.signInWithEmailAndPassword(
            formData.value.email, formData.value.password
        ).then(
            (success) => {
                console.log(success);
                this.router.navigate(['/dashboard'])
            }).catch(
            (err) => {
                console.log(err);
                this.error = err;
            })
    }
}

However, I am using Google’s Pub/Sub pull request to get messages. I have successfully gotten that to work, but I have hard coded the access_token to make it functional.

dashboard.component.ts

import { Component, Injectable, OnInit } from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import { Observable }   from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/concat';
import {IntervalObservable} from 'rxjs/observable/IntervalObservable';
import "rxjs/add/operator/mergeMap";
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

@Component({
  templateUrl: 'dashboard.component.html'
})
export class DashboardComponent implements OnInit {

  private accessToken = '[access_token]';

  private dataUrl = '[google api pull url]' + this.accessToken;

  private ackUrl = '[google api acknowledgement url]' + this.accessToken;

  dataArray = [];
  ackArray = [];

  constructor(private http: Http) {}

  ngOnInit() {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({ headers: headers });
    let body = {
      "returnImmediately": true,
      "maxMessages": 1
    };
    return Observable.concat(Observable.of(null), IntervalObservable.create(1000))
  .flatMap(() => this.http.post(this.dataUrl, JSON.stringify(body), options))
  .map(
  (res: Response) => res.json()
).subscribe(
  (res) => {

    if (res.receivedMessages){
      for (let i=0; i<res.receivedMessages.length; i++){
        const data = atob(res.receivedMessages[i].message.data);
        const ack = res.receivedMessages[i].ackId;
        if (data) {
          this.dataArray.push(data);
          console.log(this.dataArray);
          this.http.post(this.ackUrl, JSON.stringify({
            "ackIds": [
              ack
            ]
          }), options).map(
            (res: Response) => res.json()
          ).subscribe(
            (res) => {
              console.log("VALUE RECEIVED: ", res);
            });
          // this.ackArray.push(ack);
          // console.log(this.ackArray);
        }
      }
    }
  },
  (x) => {
    /* this function is executed when there's an ERROR */
    console.log("ERROR: "+x);
  },
  () => {
    /* this function is executed when the observable ends (completes) its stream */
    console.log("Completed");
  }
);

} }

I have successfully obtained the Google access_token by running the following in the command line.

gcloud auth print-access-token

Is there a way to obtain this Google access_token with Angular?

Or is there another way I should be going about this?

0 Answers
Related