【问题标题】:How to recognise an existing Google OAuth authentication made via Firebase and perform a Google Directory API request in Angular 2?如何识别通过 Firebase 进行的现有 Google OAuth 身份验证并在 Angular 2 中执行 Google Directory API 请求?
【发布时间】:2016-08-02 18:51:41
【问题描述】:

我想使用我现有的身份验证并能够使用相同的身份验证来执行对 Google Directory API 的获取请求。这是我当前的代码:

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
}

【问题讨论】:

    标签: firebase angular google-oauth google-admin-sdk google-directory-api


    【解决方案1】:

    我找到了解决方案。我需要在 GET 请求的 http 请求标头中使用当前访问令牌。

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

    【讨论】:

      【解决方案2】:

      您可以在firebaseRef 实例上利用getAuth 方法。类似的东西:

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

      请参阅此文档:https://www.firebase.com/docs/web/api/firebase/getauth.html

      【讨论】:

      • 是的,我知道,但是如何使用该身份验证数据在这种情况下执行成功的 API 请求?
      猜你喜欢
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2018-12-10
      • 2012-11-27
      • 2021-08-06
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多