![](/img/trans.png)
[英]Using Restangular to call to the Google Calendar API to grab the JSON file to inject the calendar events using Angular
[英]Using Firebase Auth to access the Google Calendar API
我正在使用AngularJS,Firebase(SDK v3)和Google Calendar API构建Web应用程序。 我正在使用Google OAuth对用户进行身份验证。 我的目的是能够从Firebase中的数据库节点创建日历事件。 到目前为止,我已设法请求访问日历范围:
_authProvider = new firebase.auth.GoogleAuthProvider();
// Get permission to manage Calendar
_authProvider.addScope("https://www.googleapis.com/auth/calendar");
_fbAuthObject.signInWithRedirect(_authProvider);
我正在使用重定向流进行身份验证,因此身份验证重定向可用:
_fbAuthObject.getRedirectResult()
.then(function _readToken(result) {
if (result.credential) {
_googleToken = result.credential.accessToken;
var authHeader = 'Bearer '+ _googleToken;
// Just a test call to the api, returns 200 OK
$http({
method: 'GET',
headers: {
'Authorization': authHeader
},
url: 'https://www.googleapis.com/calendar/v3/users/me/calendarList/primary'
})
.then(function success(response) {
console.log('Cal response', response);
},
function error(response) {
console.log('Error', response);
});
但是,在初始登录之外,似乎无法通过Firebase SDK获取Google访问令牌。 似乎只能访问Firebase JWT令牌,而不能使用Calendar API。 我可以存储访问令牌,但这不能解决刷新令牌等问题。有没有办法通过Firebase SDK获取当前的Google Access令牌,如果没有,那么有什么其他解决方案可以解决问题两次验证用户?
更新1:
好像其他人一直在努力解决与Facebook身份验证相似的问题 。 在该问题上,有一个指向Firebase文档的链接,指出Firebase身份验证不再保留访问令牌。 那么我该如何处理令牌刷新? 真的没有答案吗?
更新2:
所以,我联系了Firebase支持部门,提供了有关此问题的功能请求,他们给了我以下答案:
感谢您抽出宝贵时间给我们写信。
我明白你的观点,这确实是一个很好的建议。 我们肯定知道许多用户(例如您自己)希望OAuth功能在刷新时访问令牌。 我们正在探索潜在的解决方案,但我不能保证这是否会很快就可以使用。 我们会继续考虑您的反馈意见。 持续改进对我们的社区非常重要,所以感谢您提出这一点!
如有任何进一步的更新,请留意我们的发行说明。
因此,目前似乎无法通过Firebase SDK获取访问令牌。 我仍在努力寻找解决方法,所以如果有人对有效解决方案有任何想法,我会很高兴听到它们。 当然,如果我自己找到一个可行的解决方案,我会在这里发布。
我终于通过使用Google API JavaScript客户端处理Firebase外部的身份验证来解决这个问题。 此解决方案需要包含此处记录的Google身份验证客户端。 此处记录了手动处理Firebase登录流程。
gapi.auth2.getAuthInstance().signIn()
.then(function _firebaseSignIn(googleUser) {
var unsubscribe = firebase.auth().onAuthStateChanged(function(firebaseUser) {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!_isUserEqual(googleUser, firebaseUser)) {
// Build Firebase credential with the Google ID token.
var credential = firebase.auth.GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);
// Sign in with credential from the Google user.
return firebase.auth().signInWithCredential(credential)
.then(function(result) {
// other stuff...
});
_isUserEqual函数:
function _isUserEqual(googleUser, firebaseUser) {
if (firebaseUser) {
var providerData = firebaseUser.providerData;
for (var i = 0; i < providerData.length; i++) {
if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
providerData[i].uid === googleUser.getBasicProfile().getId()) {
// We don't need to reauth the Firebase connection.
return true;
}
}
}
return false;
}
现在我可以像这样引用访问令牌:
var user = gapi.auth2.getAuthInstance().currentUser.get();
return user.getAuthResponse().access_token;
这对我来说仍然不是理想的解决方案,但它现在可以使用,我可以对Firebase和Calendar API进行身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.