使用Angular获取Google access_token?

时间:2017-07-20 14:43:58

标签: angular firebase publish-subscribe

我有一个在Firebase上运行的Angular 4应用。我在Firebase中使用登录电子邮件和密码功能。这是完美的。

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;
            })
    }
}

但是,我正在使用Google的发布/订阅请求来获取消息。我已经成功地将其用于工作,但我已经对access_token进行了硬编码以使其正常运行。

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");
  }
);

} }

我已通过在命令行中运行以下命令成功获取了Google access_token。

gcloud auth print-access-token

有没有办法通过Angular获取此Google access_token?

或者我还有另一种方法可以解决这个问题吗?

0 个答案:

没有答案