以下代码片段有效,除非用户的浏览器配置(例如带有 Chrome 的 iOS)将它发送到 $authWithOAuthRedirect block - 然后它会失败。
失败是指 $authWithOAuthRedirect 方法有效并且用户可以批准身份验证,但它无法将范围正确发送给 Google,并且未请求电子邮件访问权限。
var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthPopup(provider, scope).then(function (authData, error) {
if (error && error.code === "TRANSPORT UNAVAILABLE") {
auth.$authWithOAuthRedirect(provider, function(error) {}, scope);
}
});
简化后,此代码将无法请求用户的电子邮件:
var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthRedirect(provider, function(error) {}, scope);
感谢您的帮助!
最佳答案
我认为问题在于您使用的是非角度版本的语法:Firebase.authWithOAuthRedirect(provider[, callback, scope])
你应该使用 AngularFire 版本: $firebaseAuth.$authWithOAuthRedirect(提供者[, 选项])
此版本返回一个 promise ,因此您的简化代码应如下所示:
var provider = 'google';
var scope = {scope:'email'};
var auth = $firebaseAuth(FirebaseInstance.firebase);
auth.$authWithOAuthRedirect(provider, scope).then(function (authObject) {
// Handle success
}, function (error) {
// Handle error
});
关于Firebase 和 angularFire - 从 Google 身份验证获取电子邮件适用于 $authWithOAuthPopup 但无法使用 $authWithOAuthRedirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30048507/