如何识别通过 Firebase 进行的现有 Google OAuth 身份验证并在 Angular 2 中执行 Google 目录 API 请求?
How to recognise an existing Google OAuth authentication made via Firebase and perform a Google Directory API request in Angular 2?
我想使用我现有的身份验证并能够使用相同的身份验证对 Google 目录 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
}
您可以在 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。
我找到了解决办法。我需要在 http 请求 headers 中使用当前的访问令牌来获取 GET 请求。
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());
});
}
我想使用我现有的身份验证并能够使用相同的身份验证对 Google 目录 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
}
您可以在 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。
我找到了解决办法。我需要在 http 请求 headers 中使用当前的访问令牌来获取 GET 请求。
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());
});
}