2016-07-05 17 views
0

我正在使用Firebase signinwithcredential进行google auth。我首先使用cordovaoath来获取id令牌并使用firebase进行签名。我在我的代码中特别指出我想获取displayName,但它总是返回null。我想知道任何人都面临同样的问题?在Ionic平台中无法通过firebase3登录获取gmail用户显示名称

enter image description here

this.loginWithGoogle = function loginWithGoogle() { 
    $cordovaOauth.google("mygoogleclient id here", ["https://www.googleapis.com/auth/calendar","https://www.googleapis.com/auth/userinfo.email", 
     "https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/plus.me", "https://www.googleapis.com/auth/plus.login", "https://www.googleapis.com/auth/drive"]).then(function (result) { 
     // "email", "profile", 
     console.log("first come here to oath"); 
     console.log("Response Object -> " + JSON.stringify(result)); 

     var unsubscribe = firebase.auth().onAuthStateChanged(function (firebaseUser) { 

      unsubscribe(); 
      // Check if we are already signed-in Firebase with the correct user. 
      // Build Firebase credential with the Google ID token. 
      var credential=''; 
      credential = firebase.auth.GoogleAuthProvider.credential(
       result.id_token); 

      // Sign in with credential from the Google user. 
      firebase.auth().signInWithCredential(credential).catch(function (error) { 

       // Handle Errors here. 
       var errorCode = error.code; 
       var errorMessage = error.message; 
       // The email of the user's account used. 

       var email = error.email; 
       // The firebase.auth.AuthCredential type that was used. 
       var credential = error.credential; 
      }); 
     }); 


    }, function (error) { 
     console.log("Error -> " + error); 
    }); 




}; 

我的返回结果的撷取画面

enter image description here

+0

的Gmail地址是在你的屏幕截图和验证。有什么问题? –

+0

我想要也获得displayName,但是displayName始终为空,即使我专门为profile.Thanks添加了范围! –

+0

有趣:我得到的显示名称,即使没有指定任何额外的范围:'displayName:“Frank van Puffelen”' –

回答

0

您应该请求访问令牌,而不是一个ID令牌,如果您要求额外的范围。

要解决你的代码,改变这一行:

var credential = firebase.auth.GoogleAuthProvider.credential(
          null, result.access_token); // <- access_token 
相关问题